WP8 LongListSelector SelectedItem 不可绑定 [英] WP8 LongListSelector SelectedItem not bindable

查看:15
本文介绍了WP8 LongListSelector SelectedItem 不可绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WP8 中,他们忘记提供 SelectedItem 作为依赖属性,因此我无法绑定到它.我用这个解决了这个问题:http://dotnet-redzone.blogspot.com/2012/11/windows-phone-8longlistselector.html

In WP8, they forgot to provide SelectedItem as a dependency property, hence I'm not able to bind to it. I fixed that using this: http://dotnet-redzone.blogspot.com/2012/11/windows-phone-8longlistselector.html

这样做时,我注意到我无法从 ViewModel 重置属性,即如果我在 ViewModel 中将项目设置为 null,它不会影响 UI.我已经在 UI 中提供了两种方式绑定,但仍然在 ViewModel 中将项目设置为 null 不会更改 LongListSelector 中的选定项目.我也不想使用 SelectionChanged 事件,因为我在 WP7.5 应用程序和 WP8 应用程序之间共享 ViewModel,因此我想尽可能多地推送到 ViewModel.有解决办法吗?

On doing so, I'm noticing that I'm not able to reset the property from the ViewModel, i.e. if I set the item to null in the ViewModel, it does not impact the UI. I have already provided two way binding in the UI but still setting the item to null in the ViewModel does not change the selected item in the LongListSelector. I also don't want to use the SelectionChanged event as I'm sharing ViewModels between WP7.5 app and a WP8 app, hence I want to push as much as I can into the ViewModel. Is there a solution for this?

推荐答案

您使用的自定义 LongListSelector 类似乎没有正确处理 setter.

It appears that the custom LongListSelector class that you are using does not handle the setter properly.

将 OnSelectedItemChanged 回调替换为以下内容:

Replace the OnSelectedItemChanged callback with the following:

    private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var selector = (LongListSelector)d;
        selector.SetSelectedItem(e);
    }

    private void SetSelectedItem(DependencyPropertyChangedEventArgs e)
    {
        base.SelectedItem = e.NewValue;
    }

这篇关于WP8 LongListSelector SelectedItem 不可绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆