R – Why does the WPF Databinding not update after the collection is Filtered

collectionviewsourcedata-bindingfilternetwpf

Why does my WPF ContextMenu databinding not update as expected when the collection is updated, however the ItemTemplate is displaying the context menu text correctly?

Within the code below this works when I don't try update the ObservableCollection. When the underlying ObservableCollection is updated the DataTemplate updates without problems and displays the new MenuItem text as expected. However the MenuItem.Tag is returning Nothing after the refresh occurs?

The data bound MenuItem.Tag works when the collection is first loaded however not after I update. Any ideas on how I can locate this error? Partial XAML code is shown below:

<ListBox ItemsSource="{Binding Source={StaticResource ListBoxViewSource}}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                <WrapPanel.ContextMenu>
                    <ContextMenu>
                        <Separator/>
                        <MenuItem ItemsSource="{Binding Source={StaticResource ContextViewSource}}" ItemTemplate="{StaticResource DataTemplate}">
                            <MenuItem.Tag>
                                <Binding Path="ID" Source="{StaticResource ContextViewSource}"/>
                            </MenuItem.Tag>

Update #1: The issue seems to be related to the CollectionViewSource and the Filter not updating the MenuItem.Tag binding after filtering has occurred and removed the item. I have added CollectionViewSource.View.Refresh() where the collection would be changing however this still does not fix the issue.

Update #2 I have already implemented INotifyPropertyChanged which does not help with the Filtering issue. If I remove the Filter on the CollectionViewSource then the issue doesn't occur. I also added a converter to the MenuItem.Tag binding and this doesn't get called after the filtering is applied and the MenuItem.Tag is then set to Nothing.

Best Answer

The observable collection sends property change notification events when items are added or removed from the collection, it doesn't send updates when the content of the items is changed. Try sending the notifications yourself and see if your WPF view updates.

Related Topic