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

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

    Please follow the below code. I have succeeded with the below code.

    Please let me know if changes are required.

     private void grdEmployee_PreviewKeyDown(object sender, KeyEventArgs e)
        {
    
            if (e.Device.Target.GetType().Name == "DataGridCell")
            {
                if (e.Key == Key.Delete)
                {
                    MessageBoxResult res = MessageBox.Show("Are you sure want to delete?", "Confirmation!", MessageBoxButton.YesNo,MessageBoxImage.Question);
                    e.Handled = (res == MessageBoxResult.No);
                }
            }
        }
    

提交回复
热议问题