1

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.

Rico Suter
  • 11,548
  • 6
  • 67
  • 93
HelpMatters
  • 1,299
  • 1
  • 13
  • 32

1 Answers1

0

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

HelpMatters
  • 1,299
  • 1
  • 13
  • 32
  • 1
    I think you should be looking at `SelectedItems.CollectionChanged` event. With this attached behavior - the selected items collection isn't replaced so your property setter isn't getting set. Instead the collection is being updated in place so if you watch the `CollectionChanged` event you can see what was added or removed in the collection. – Filip Skakun Aug 22 '14 at 15:04