I wish to recalculate things everytime a DataGrid gets more rows or some are removed. I tried to use the Loaded event, but that was fired only once.
I found AddingNewItem, but that is fired before it has been added. I need to do my stuff afterwards.
There’s also LayoutUpdated, which works, but I’m afraid it’s not wise to use it, because it fires way too often for my purposes.
If your
DataGridis bound to something, I think of two ways of doing this.You could try getting the
DataGrid.ItemsSourcecollection, and subscribing to itsCollectionChangedevent. This will only work if you know what type of collection it is in the first place.The other solution would be to use an Event System such as Microsoft Prism’s
EventAggregatoror MVVM Light’sMessenger. This means yourViewModelwould broadcast aDataCollectionChangedevent message anytime the bound collection changes, and yourViewwould subscribe to receive these messages and execute your code anytime they occur.Using
EventAggregatorUsing
Messenger