Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle a CompositeCollection with CollectionView features?

Is there a way to get notified when CompositeCollection's current location changes?

I need to have the CompositeCollection monitored by a CollectionView, any ideas are welcommed.

like image 897
Shimmy Weitzhandler Avatar asked Jan 17 '10 00:01

Shimmy Weitzhandler


1 Answers

You can detect when the current item has changed by monitoring 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, the message box displays.

Regarding filtering, sorting and grouping, as per Aron's answer these are not available on a view over a CompositeCollection. But for the record here are the ways you can detect changes for views that do support these features:

  • It looks like you'll get a CollectionChanged event when the filter changes, though I can't find this documented.
  • SortDescriptions is SortDescriptionCollection which is INotifyCollectionChanged, so hook up a CollectionChanged event handler on the SortDescriptions property.
  • GroupDescriptions is ObservableCollection<GroupDescription>, so hook up a CollectionChanged event handler on the GroupDescriptions property.
like image 193
itowlson Avatar answered Dec 12 '22 06:12

itowlson