What is the “pressed the delete key” event for the WPF Datagrid?

后端 未结 6 905
暗喜
暗喜 2020-12-13 04:16

I want to enable the user to highlight a row on the WPF DataGrid and press delete key to delete the row.

  • the functionality is already
6条回答
  •  [愿得一人]
    2020-12-13 04:28

    The RemovedItems items reflects the items removed from the selection, and not from the grid.

    Handle the PreviewKeyDown event, and use the SelectedItems property to delete the selected rows there:

    private void PreviewKeyDownHandler(object sender, KeyEventArgs e) {
        var grid = (DataGrid)sender;
        if ( Key.Delete == e.Key ) {
            foreach (var row in grid.SelectedItems) {
                ... // perform linq stuff to delete here
            }
        }
    }
    

提交回复
热议问题