I have anObservableCollection for my underlying list of items. I have 2 CollectionViewSource that are different filters of the data. I want any changes that happen to be reflected in the CollectionViewSource.
In my view model constructor, I set the filter predicates. I then get the data an add it to the ObservableCollection. I am subscribed to the CollectionChanged event, and am refreshing the CVS there.
I am seeing some really weird things with this though. Either the items don’t show up in my ListBox that is bound to the CVS, or duplicates will show up. If I call Refresh on the CVS outside of the CollectionChanged event after I change the ObservableCollection, everything seems to work fine. I would really like to be able to just refresh when the collection changes and not worry about having to call refresh every time something is done that changes it.
public MyViewModel()
{
oc.CollectionChanged += OcCollectionChanged;
cvs1.Source = oc;
cvs1.View.Filter = new Predicate<object>( ... );
cvs2.Source = oc;
cvs2.View.Filter = new Predicate<object>( ... );
foreach( var data in myData )
{
oc.Add( data );
}
}
private void OcCollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
{
cvs1.View.Refresh();
cvs2.View.Refresh();
}
CollectionViewSourcedoesn’t implementINotifyPropertyChanged, so to get any underlying data changes into the UI you need to callRefreshon the View as you are already doing.CollectionViewSourceis also data source agnostic, so the fact that the source is anObservableCollectionthat raises property change notification is immaterial, because theCollectionViewSourceis not listening.I think the solution you have of manually refreshing the views when the collection changes is the best you’re going to get without re-thinking your data structures.