I am using WPF DataGrid for displaying a list of items. I had to modify an item, but the item had no INotifyPropertyChanged event on that property (it was a collection).
So my first idea was:
- Update the list behind ItemsSource
- Set ItemsSource property to null to force unbinding
- Set ItemsSource property to the new collection
- Set the SelectedItem to the new item
- Call ScrollIntoView() on the new item
Well, I got a huge exception at step 5. There must be a simpler solution. And there is.
The DataGrid generates a view of the ItemsSource. All I have to do is call the Items.Refresh() method, and it will rebind to the ItemsSource. Everything will be rebound, all updates will be visible. Selected item kept, visibility kept.
The method of items.Refresh to force an update of a WPF Datagrid is often repeated in the web, but doesn't actually work. If I had the answer I would repeat it here, but I am still looking for one.
ReplyDeleteThis fixed it for me: http://stackoverflow.com/questions/2472680/refresh-datagrid-on-view-from-viewmodel/7603765#7603765
ReplyDeleteGreat post, much appreciate the time you took to write this.
ReplyDelete