Edited to address F Ruffell’s answer
I have the following xaml
<StackPanel>
<ListBox x:Name="_list1"/>
<ListBox x:Name="_list2"/>
</StackPanel>
and this code-behind:
var ints = new[] { 1, 2, 3 };
_list1.ItemsSource = ints;
_list2.ItemsSource = ints;
_list1.Items.Filter = i => ((int)i) < 2;
For some reason after setting filter only for the first ListBox both lists are filtered. I expect the lists to have completely different CollectionViews and indeed _list1.Items != _list2.Items. Meanwhile setting filter to one of them also somehow sets that very filter to the other.
The question is why and how are CollectionViews synchronized?
They are synchronized because even though both
ListBoxeshave differentItems, they share the sameCollectionView, which is the default view for the source collection.The
Itemsproperty ofItemsControlis of typeItemCollectionand theCollectionViewproperty ofItemCollectionis internal so we can’t access it directly to verify that this is true. However, we can just enter these three values in the debugger to verify this, they all come out as trueAlternatively, we can use reflection to do the comparison in code
The way to work around this has already been posted by F Ruffell, create a new
ListCollectionViewasItemsSourcefor eachListBox.Also note that after this, the 3 comparisons above comes out as false