WPF DataGrid Notification of Changes

I have a DataGrid associated with an ICollectionView (with the filter enabled). In particular, I set view.Filter = SomeFilteringFunction , which uses the public DateTime DateFrom { get... set... } property public DateTime DateFrom { get... set... } , also associated with DatePicker .

Well and now, when I change the DatePicker , the associated DateFrom property DateFrom correctly, but the DataGrid is not explicitly filtered.

What is the best way to notify a DataGrid for an update?

Thank you in advance!

James

+4
source share
2 answers

You should not bind directly to the ICollectionView, rather you will bind to the original collection, and then apply the filter to the ICollectionView returned by CollectionViewSource.GetDefaultView.

 <DataGrid ItemsSource="{Binding MyCollection}" /> 
 // should raise INotityPropertyChange.PropertyChanged public ObservableCollection<Entity> MyCollection { get; set; } MyCollection = new ObservableCollection<Entity>(ctx.EntitySet)); ICollectionView view = CollectionViewSource.GetDefaultView(MyCollection); view.Filter = SomeFilteringFunction; 

Then, when the value of the DatePicker parameter changes, you need to tell ICollectionView to update.

 ICollectionView view = CollectionViewSource.GetDefaultView(MyCollection); view.Refresh(); 
+4
source

You can subscribe to the PropertyChanged event (which I assume you implemented it in the class) and update the view in the handler:

 var view = CollectionViewSource.GetDefaultView(Collection); if (view != null) { view.Refresh(); } 

Not sure if there is a cleaner way, but I'm pretty sure you need to make this Refresh call at one point.

0
source

All Articles