5
votes

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.

1
DataGrid doesn't looses focus, deleted row (item) does. And to implement behavior (which is not default) you have to do some work, e.g.: disable deleting, manually process key presses, handle Delete key to delete row/rows and apply behavior you want.Sinatr
How do you fill your DataGrid? Is it by binding it's ItemsSource?Tomtom
@Sinatr I can't manually handle, I did keybinding for delete for some reason.RainCast
@Tomtom, yes, the ItemsSource did bind to data defined in ViewModel, and the DataGrid is editable.RainCast
Then the DataGrid does automatically set the focus to the next rowTomtom

1 Answers

1
votes

What are you binding your DataGrid to? Have you tried a CollectionView (MSDN)? That has props like CurrentPosition and the MoveCurrentToNext() method. You could get the current position of the row being deleted and then locate the next one based on the sort order of the CollectionView.

Update: If you're doing MVVM, disable the DataGrid's delete (see this answer) and have your VM remove the item from the collection. You'll know which item is being removed so you should then be able to figure out what the next (or prior) item is. Have your VM expose a SelectedItem property which you'll bind to the DataGrid.SelectedItem. Set the SelectedItem on your VM and the row should be selected in the DataGrid.

It sounds like the WinForms Datagrid handled a lot of this out of the box so you probably feel like you're reinventing the wheel and that this is a lot of work for something that just worked automatically in the "old" UX technology.