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
If you want you can go down the RowUnloading
route as others have described here, however note that this event fires also every time a row is losing focus.
However by playing around I found that when a row is removed the SelectedItem
property of the grid is null while the CurrentItem
property is not null, and so far I have seen this combination only for a deleted row, (although I can't guarantee that I have not missed an exotic situation... however for the basic situations of moving away from the row I have not seen it so far).
So when can use the following code to filter for deleted rows only:
private void CategoriesGrid_UnloadingRow(object sender, DataGridRowEventArgs e)
{
if (((DataGrid)sender).SelectedItem != null || ((DataGrid)sender).CurrentItem == null)
{
return;
}
// The rest of your code goes here
}