Does WPF DataGrid fire an event when a row is added / removed?

前端 未结 7 612
失恋的感觉
失恋的感觉 2021-02-06 05:48

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

7条回答
  •  盖世英雄少女心
    2021-02-06 06:43

    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
    }
    

提交回复
热议问题