本文介绍了无法将 LongListSelector.SelectedItem 绑定到 MVVM 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Visual Studio 2013 和 Window Phone 8 SDK 我无法获得 LongListSelectorSelectedItem 属性以正确绑定到 MVVM 属性.

Using Visual Studio 2013 and the Window Phone 8 SDK I cannot get the SelectedItem property of the LongListSelector to properly bind to an MVVM property.

这似乎与控件包含在 SDK 中之前存在但标记为已修复的错误相同.http://silverlight.codeplex.com/workitem/9360

It appears to be an identical issue to a bug that existed in the control prior to its inclusion in the SDK but which is marked as fixed. http://silverlight.codeplex.com/workitem/9360

是否还有其他人遇到此问题并知道修复/更新版本?

Is anyone else experiencing this and know of a fix/updated version?

我目前正在使用代码隐藏解决方法

I am currently using a code behind workaround

    private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        LongListSelector s = sender as LongListSelector;
        var vm = DataContext as ViewModel.MainViewModel;
        Debug.Assert(vm != null);

        vm.SelectedLegislator = s.SelectedItem;
    }

推荐答案

为了获得被选中到 ViewModel 的项目,我总是使用 LongListSelector 扩展 - 代码可以在这里找到:https://gist.github.com/Depechie/7524630

To get the item that was selected to the ViewModel, I'm always using a LongListSelector Extension - the code can be found here: https://gist.github.com/Depechie/7524630

您需要做的是将其添加到 LongListSelector 的 XAML 中:

What you need to do is add it to the XAML of your LongListSelector:

<phone:LongListSelector x:Name="List" ext:LongListSelectorExtension.Command="{Binding *YOURVIEWMODELCOMMAND*}" />

视图模型上的命令将在 LongListSelector 上接收您的项目源的对象类型

The command on the viewmodel will receive the object type of your item source on the LongListSelector

这篇关于无法将 LongListSelector.SelectedItem 绑定到 MVVM 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:41