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

后端 未结 6 906
暗喜
暗喜 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:34

    XAML

    
    

    Code behind

    private void Grid_PreviewCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
      DataGrid grid = (DataGrid)sender;
      if (e.Command == DataGrid.DeleteCommand)
      {
        if (MessageBox.Show(String.Format("Would you like to delete {0}", (grid.SelectedItem as Person).FirstName), "Confirm Delete", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
          e.Handled = true;
      }
    }
    

提交回复
热议问题