title: What is the "pressed the delete key" event for the WPF Datagrid?
body: <p>I want to enable the user to <strong>highlight a row on the WPF DataGrid and press delete key</strong> to delete the row.</p>
  
  <ul>
  <li>the functionality is already <strong>built into the UI</strong> of the grid, so to the user, the row disappears</li>
  <li>I currently handle this on the <strong>SelectionChanged event</strong> (code below)</li>
  <li>I loop through all the "e.RemovedItems" and delete them with LINQ</li>
  </ul>
  
  <p><strong>Problem is:</strong> even when you simply <em>select</em> a row and move off of it, selection change is fired and that row is in e.RemovedItems (which is odd, why would simply selecting something put it in a RemovedItems container?).</p>
  
  <p>So I am looking for a <strong>DeleteKeyPressed</strong> event so I can simply handle it. What is that event called?</p>
  
  <p>I am using the March 2009 toolkit.</p>
  
  <p><strong>XAML:</strong></p>
  
  <pre><code>&lt;Grid DockPanel.Dock="Bottom"&gt;
      &lt;toolkit:DataGrid x:Name="TheDataGrid" 
                        SelectionChanged="TheDataGrid_SelectionChanged"
                        AutoGenerateColumns="True"
                        RowEditEnding="TheDataGrid_RowEditEnding"/&gt;
  </code></pre>
  
  <p><strong>code-behind:</strong></p>
  
  <pre><code>private void TheDataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  {
      if (e.RemovedItems.Count &gt; 0)
      {
          Message.Text = "The following were removed: ";
          foreach (object obj in e.RemovedItems)
          {
              Customer customer = obj as Customer;
              Message.Text += customer.ContactName + ",";
              _db.Order_Details.DeleteAllOnSubmit(
                  customer.Orders.SelectMany(o =&gt; o.Order_Details));
              _db.Orders.DeleteAllOnSubmit(customer.Orders);
              _db.Customers.DeleteOnSubmit(customer);
          } 
      }
  
      try
      {
          _db.SubmitChanges();
      }
      catch (Exception ex)
      {
          Message.Text = ex.Message;
      }
  }
  </code></pre>
  
  <hr/>
  
  <h1>ANSWER:</h1>
  
  <p>Thanks lnferis, that was exactly what I was looking for, here is my finished delete handling event for the datagrid, note the KeyDown event doesn't fire for some reason.</p>
  
  <p><strong>XAML:</strong></p>
  
  <pre><code>&lt;toolkit:DataGrid x:Name="TheDataGrid" 
                    KeyDown="TheDataGrid_KeyDown"
                    PreviewKeyDown="TheDataGrid_PreviewKeyDown"
                    AutoGenerateColumns="True"
                    RowEditEnding="TheDataGrid_RowEditEnding"/&gt;
  </code></pre>
  
  <p><strong>code-behind</strong></p>
  
  <pre><code>private void TheDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
  {
      if (e.Key == Key.Delete)
      {
          var grid = (DataGrid)sender;
  
          if (grid.SelectedItems.Count &gt; 0)
          {
              string checkMessage = "The following will be removed: ";
  
              foreach (var row in grid.SelectedItems)
              {
                  Customer customer = row as Customer;
                  checkMessage += customer.ContactName + ",";
              }
              checkMessage = Regex.Replace(checkMessage, ",$", "");
  
              var result = MessageBox.Show(checkMessage, "Delete", MessageBoxButton.OKCancel);
              if (result == MessageBoxResult.OK)
              {
                  foreach (var row in grid.SelectedItems)
                  {
                      Customer customer = row as Customer;
                      _db.Order_Details.DeleteAllOnSubmit(
                          customer.Orders.SelectMany(o =&gt; o.Order_Details));
                      _db.Orders.DeleteAllOnSubmit(customer.Orders);
                      _db.Customers.DeleteOnSubmit(customer);
                  }
                  _db.SubmitChanges();
              }
              else
              {
                  foreach (var row in grid.SelectedItems)
                  {
                      Customer customer = row as Customer;
                      LoadData();
                      _db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, customer); //TODO: this doesn't refresh the datagrid like the other instance in this code
                  }
              }
          }
      }
  }
  
  private void TheDataGrid_KeyDown(object sender, KeyEventArgs e)
  {
      Console.WriteLine("never gets here for some reason");
  }
  </code></pre>