I am using WPF DataGrid row deletion by keyboard "Delete" key press. However, after the row is deleted, DataGrid lost its focus, and DataGrid.SelectedIndex = -1.
Compared to WinForm datagrid, after a row is deleted, the focus automatically shift to the next focusable row, which is much more reasonable.
By "next focusable", I mean, e.g.:
Row A
Row B
Row C
If we delete Row A, the next focusable will be Row B.
If we delete Row B, the next focusable will be Row C.
If we delete Row C, the next focusable will be Row B (the last row).
How can we achieve the same effect in WPF datagrid?
By the way, the following is an example that focuses on the first row after deletion, which is not ideal, since we wanted it to be the "next focusable row".
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count > 0)
{
DataGrid grid = sender as DataGrid;
if (grid.SelectedIndex == -1)
{
grid.SelectedCells.Clear();
grid.SelectedIndex = 0;
}
}
}
Any ideas?
Thanks.