How to bind my ObservableCollection two way to a ListView Extension WinRT Xaml Toolkit

跟風遠走 提交于 2019-12-11 08:43:52

问题


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

Here is my XAML

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

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");
            }
        }
    }

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.

Am I doing something wrong.

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


回答1:


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

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.

Here is how you instantiate the ObservableCollection.

SelectedItems = new ObservableCollection<MyItems>();

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



来源:https://stackoverflow.com/questions/25441255/how-to-bind-my-observablecollection-two-way-to-a-listview-extension-winrt-xaml-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!