title: Programmatically run WPF event handler
body: <p>I have two following events for wpf datagrid:</p>
  
  <p>1-CellEditEnding</p>
  
  <p>2-PreviewKeyDown</p>
  
  <p>When I finish editing in any of the cell in datagrid, first PreviewKeyDown event runs and then CellEditEnding. But I need to run CellEditEnding first and then PreviewKeyDown. So i was calling PreviewKeydown event programmatically in the CellEditEnding event by writing the following code.</p>
  
  <pre><code>private void maingrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
  {
      TextBox t = e.EditingElement as TextBox;
      DataGridColumn dgc = e.Column;
      if ((string)dgc.Header == "Product Id")
      {
          if (vm_order.PopulateProductRow(maingrid.SelectedIndex, Convert.ToInt32(t.Text)) == false)
          {
              MessageBox.Show("Product does not exists           ", "Message", MessageBoxButton.OK);
          }
      }
  
      **maingrid.PreviewKeyDown += new maingrid_PreviewKeyDown(maingrid_PreviewKeyDown);**
  }
  </code></pre>
  
  <p>Please view the last line of code where i am calling PrevewKeyDownEvent programmatically which is giving error. </p>
  
  <p>I have seen an example for mouse_up event for wpf datagrid which is working very nicely. Following is the code for reference:</p>
  
  <pre><code>public EventsSample()
  {
      InitializeComponent();
      pnlMainGrid.MouseUp += new MouseButtonEventHandler(pnlMainGrid_MouseUp);
  }
  
  private void pnlMainGrid_MouseUp(object sender, MouseButtonEventArgs e)
  {
      MessageBox.Show("You clicked me at " + e.GetPosition(this).ToString());
  }
  </code></pre>
  
  <p>So why cannot I use this for PreviewKeyDown event?</p>