The question is why and how are CollectionViews synchronized?
They are synchronized because although both ListBoxes have different Items , they have the same CollectionView , which is the default view for the source collection.
The Items PropertyControl property is of the ItemCollection type, and the CollectionView ItemCollection property is internal, so we cannot access it directly to make sure this is true. However, we can simply enter these three values ββin the debugger to check this, they all look like true
_list1.Items.CollectionView == _list2.Items.CollectionView // true _list1.Items.CollectionView == CollectionViewSource.GetDefaultView(ints) // true _list2.Items.CollectionView == CollectionViewSource.GetDefaultView(ints) // true
Alternatively, we can use reflection to compare in code.
PropertyInfo collectionViewProperty = typeof(ItemCollection).GetProperty("CollectionView", BindingFlags.NonPublic | BindingFlags.Instance); ListCollectionView list1CollectionView = collectionViewProperty.GetValue(_list1.Items, null) as ListCollectionView; ListCollectionView list2CollectionView = collectionViewProperty.GetValue(_list2.Items, null) as ListCollectionView; ListCollectionView defaultCollectionView = CollectionViewSource.GetDefaultView(ints) as ListCollectionView; Debug.WriteLine(list1CollectionView == list2CollectionView); Debug.WriteLine(list1CollectionView == defaultCollectionView); Debug.WriteLine(list2CollectionView == defaultCollectionView);
A way around this has already been published by F Ruffell, create a new ListCollectionView as an ItemsSource for each ListBox .
_list1.ItemsSource = new ListCollectionView(ints); _list2.ItemsSource = new ListCollectionView(ints);
Also note that after this 3 comparisons above come out as false
source share