Is this the expected sequence of operations for what seems like a very normal use case, or is there a better way to do this?
No, this is not an ordinary method of working with DataGrids in WPF. This seems like normal for WinForms, but not for WPF. :)
A typical DataGrid.ItemsSouce binding method is to build a class that inherits INotifyPropertyChanged to represent each row of data and binds DataGrid.ItemsSource to the ObservableCollection<T> this class
For example, you might have something like
public class WatchListModel : INotifyPropertyChanged {
This class should implement INotifyPropertyChanged , and properties should raise notification of changes, however I left it for simplicity. If you need an example, check out here .
Then you will have an ObservableCollection<WatchListModel> that you will also bind your DataGrid.ItemsSource
public ObservableCollection<WatchListModel> WatchListCollection { get; set; }
and
<DataGrid ItemsSource="{Binding WatchListCollection}" ... />
And if you need real-time updates, you need to add an event handler handler for WatchListCollection to handle add / remove and add a modified property handler to every element processed when it is modified
public MyViewModel() { WatchListCollection = new ObservableCollection<WatchListModel>(); // Hook up initial changed handler. Could also be done in setter WatchListCollection.CollectionChanged += WatchListCollection_CollectionChanged; } void WatchListCollection_CollectionChanged(object sender, CollectionChangedEventArgs e) { // if new itmes get added, attach change handlers to them if (e.NewItems != null) foreach(WatchListModel item in e.NewItems) item.PropertyChanged += WatchListModel_PropertyChanged; // if items got removed, detach change handlers if (e.OldItems != null) foreach(WatchListModel item in e.OldItems) item.PropertyChanged -= WatchListModel_PropertyChanged; // Process Add/Remove here } void WatchListModel_PropertyChanged(object sender, PropertyChangedEventArgs e) { // Process Update here }
This will be the correct WPF way :)