You can detect when the current item has changed by tracking the ICollectionView.CurrentChanged event of your CollectionView. The following code works for me:
CompositeCollection cc = new CompositeCollection(); cc.Add(new CollectionContainer { Collection = new string[] { "Oh No!", "Fie" } }); cc.Add(new CollectionContainer { Collection = new string[] { "Zounds", "Ods Bodikins" } }); CollectionViewSource cvs = new CollectionViewSource { Source = cc }; // Subscribing to CurrentChanged on the ICollectionView cvs.View.CurrentChanged += (o, e) => MessageBox.Show("current changed"); lb.ItemsSource = cvs.View; // lb is a ListBox with IsSynchronizedWithCurrentItem="True"
When I change the selection in the ListBox, a message box appears.
As for filtering, sorting and grouping, according to Aron's answer, they are not available when viewing over CompositeCollection. But for the record here, you may find changes to views that support these functions:
- It looks like you will get the CollectionChanged event when the filter changes, although I cannot find this document.
- SortDescriptions is a SortDescriptionCollection, which is an INotifyCollectionChanged, so hook up the CollectionChanged event handler in the SortDescriptions property.
- GroupDescriptions
ObservableCollection<GroupDescription> , so enable the CollectionChanged event handler in the GroupDescriptions property.
itowlson
source share