DataGrid BeginEdit DataGridTemplateColumn Problem

Mar 19, 2009 at 10:01 PM
Hi,

I want my data grid to enter edit mode whenever a user tabs into a new cell.

I can get this to work for DataGridTextColumn's by handling the grid.CurrentCellChanged event and calling grid.BeginEdit()

However, this does not seem to work for a DataGridTemplateColumn. I tried a simple case of a DataGridTemplateColumn whose celledittemplate is just a textbox.

Is there anything special required to beginEdit for a template column?

thanks,
Adam
Mar 20, 2009 at 4:54 PM
Found this workaround for now:

private void grid_CurrentCellChanged(object sender, EventArgs e)
{
  grid.BeginEdit();
		
  // After begin edit shouldnt be a DataGridCell so if it is it means we have a template 
  // column and therefore need another tab to enter edit mode
  if (Keyboard.FocusedElement is DataGridCell)
  {
    // Send the grid another tab press
    var keyEvt = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Tab){RoutedEvent = Keyboard.KeyDownEvent};
    InputManager.Current.ProcessInput( keyEvt);
  }
}
Oct 23, 2009 at 7:16 AM
adamtaub wrote:
Found this workaround for now:

private void grid_CurrentCellChanged(object sender, EventArgs e)
{
  grid.BeginEdit();
		
  // After begin edit shouldnt be a DataGridCell so if it is it means we have a template 
  // column and therefore need another tab to enter edit mode
  if (Keyboard.FocusedElement is DataGridCell)
  {
    // Send the grid another tab press
    var keyEvt = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Tab){RoutedEvent = Keyboard.KeyDownEvent};
    InputManager.Current.ProcessInput( keyEvt);
  }
}

 

This only works if you are tabbing forward through the grid. If you are tabbing backwards (shift-tab), then it will actually end up skipping the cell. A better way to do this is with a traversal request (this is what the grid does internally when it detects a tab key press):

DataGridCell FocusedCell = Keyboard.FocusedElement as DataGridCell;

if (FocusedCell != null)

{

    // if we try to send a simulated "tab" key, and the shift button is being held down,

    // the focus will skip in an unintended way. By using a traversal request, we can

    // tell the cell to send focus to the next control in the focus sequence - which in

    // the case of a template column will be the first visible child control

    TraversalRequest Traversal = new TraversalRequest(FocusNavigationDirection.Next);

    FocusedCell.MoveFocus(Traversal);

}

 

Dec 15, 2009 at 6:23 AM

Keith's answer worked pretty well, I wound up with this: 

    private void grid_CurrentCellChanged(object sender, EventArgs e)
      {
      DataGrid dg = sender as DataGrid;
      dg.BeginEdit();
      DataGridCell FocusedCell = Keyboard.FocusedElement as DataGridCell;
      if (FocusedCell != null)
        {
        TraversalRequest Traversal = new TraversalRequest(FocusNavigationDirection.Next);
        FocusedCell.MoveFocus(Traversal);
        }
      }

The other method I found of getting the textbox and setting focus to it seemed to work at first, but was unreliable.