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

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

    You want to handle the KeyUp or KeyDown event and check the pressed Key for Delete.

    private void OnKeyDown(object sender, KeyEventArgs e) {
      if ( Key.Delete == e.Key ) {
        // Delete pressed
      }
    }
    
    0 讨论(0)
  • 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
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-13 04:31

    A little late to the party, but to get Inferis answer working:

    Dim isEditing = False
    AddHandler dg.BeginningEdit, Sub() isEditing = True
    AddHandler dg.RowEditEnding, Sub() isEditing = False
    AddHandler dg.PreviewKeyDown, Sub(obj, ev) 
      If e.Key = Key.Delete AndAlso Not isEditing Then ...
    

    This fixes epalms comment: "if you're editing a cell and use the delete key to remove some characters in the cell, you'll end up deleting the whole row"

    0 讨论(0)
  • 2020-12-13 04:34

    XAML

    <DataGrid ItemsSource="{Binding}" CommandManager.PreviewCanExecute="Grid_PreviewCanExecute" />
    

    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;
      }
    }
    
    0 讨论(0)
  • 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);
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-13 04:50

    What are you binding your DataGrid to? Ideally, you should react to CollectionChanged events on the collection you are binding to. That way, your logic (deletion of removed items) will be separated from your UI.

    You can build an Observable collection containing your objects and bind it to ItemsSource just for that purpose if the original collection does not have the necessary events.

    It might not suit your specific setup, but that's how I usually do it.

    0 讨论(0)
提交回复
热议问题