如何将我的 ObservableCollection 以两种方式绑定到 ListView 扩展 WinRT Xaml 工具包 [英] How to bind my ObservableCollection two way to a ListView Extension WinRT Xaml Toolkit

查看:28
本文介绍了如何将我的 ObservableCollection 以两种方式绑定到 ListView 扩展 WinRT Xaml 工具包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 ListView 的 Windows 8.1 应用程序,我正在使用 WinRt Xaml 工具包中的 ListViewExtensions(从 Nuget 获得最新)来绑定 BindableSelection

I have a Windows 8.1 application with a ListView and I am using ListViewExtensions from WinRt Xaml Toolkit(Obtained latest from Nuget) to bind BindableSelection

这是我的 XAML

    <ListView
        ItemsSource="{Binding AllItems}"
        SelectionMode="Multiple"
        ext:ListViewExtensions.BindableSelection="{Binding SelectedItems, Mode=TwoWay}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

在我的 ViewModel 中,我有以下 ObservableCollection,我已将我的 xaml 绑定到

In my ViewModel I have the following ObservableCollection which I have bound my xaml to

  private ObservableCollection<string> _SelectedItems;
    public ObservableCollection<string> SelectedItems
    {
        get { return _SelectedItems; }
        set
        {
            if (value != _SelectedItems)
            {
                _SelectedItems = value;
                NotifyPropertyChanged("SelectedItems");
            }
        }
    }

我在 ObservableCollection 的 get 和 set 上设置了断点.将在我的视图加载后立即调用 get,但即使我选择了 ListView 的多个项目,也不会调用该集合.

I have put breakpoints on the get and set of my ObservableCollection. The get will be called as soon as my View loads, but the set is never called even though I select multiple items of my ListView.

我是不是做错了什么.

如果有人能指出我正确的方向,我会很高兴.提前致谢.

I would be very glad if someone can point me in the right direction. Thanks in Advance.

推荐答案

意识到我的错误.我从未为 ObservableCollections SelectedItems 创建对象.

Realized my mistake. I have never created the object for ObservableCollections SelectedItems.

应该在某个时候为 ObservableCollection 创建对象,否则 XAML 将绑定到显然无法更新的空对象引用.

One should create the object for the ObservableCollection at some point otherwise the XAML will be binding to a null object reference which obviously cannot be updated.

这是实例化 ObservableCollection 的方法.

Here is how you instantiate the ObservableCollection.

SelectedItems = new ObservableCollection<MyItems>();

但是我仍然无法命中 ObservableCollection 的 set 函数的断点.我相信这是 Observable 的默认行为.如果有人可以对此发表评论,我会很高兴.

However I am still unable to hit the breakpoint of the set function of the ObservableCollection. I believe that this is the default behavior of the Observable. Would be glad if someone can comment on that.

尽管如此,这个特定问题的问题已经解决了.谢谢

Nevertheless the problem for this particular question is solved. Thanks

这篇关于如何将我的 ObservableCollection 以两种方式绑定到 ListView 扩展 WinRT Xaml 工具包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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