how to set focus to particular cell of WPF toolkit datagrid
I have datagrid with TextBox in DataTemplate of DataGridTemplateColumn. Also i am using Enter instead of Tab to focus TextBox
private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
e.Handled = true;
var TabKey = new KeyEventArgs(e.KeyboardDevice, e.InputSource, e.Timestamp, Key.Tab);
TabKey.RoutedEvent = Keyboard.KeyDownEvent;
InputManager.Current.ProcessInput(TabKey);
}
}
I solve focus problem with combination of this code:
dataGrid.Focus();
//In case of more columns
//dataGrid.CurrentCell = new DataGridCellInfo(dataGrid.Items[0], dataGrid.Columns[1]);
dataGrid.BeginEdit();
(Keyboard.FocusedElement as UIElement).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
You need to set the current cell to the one you want edited and then call BeginEdit in your Loaded handler:
dataGrid1.CurrentCell = new DataGridCellInfo(
dataGrid1.Items[0], dataGrid1.Columns[3]);
dataGrid1.BeginEdit();
If you give your DataGridTextColumn a name in XAML you can use that name rather than Columns[3]
.
Use this code to move the scroll view to a specific cell:
dgv.ScrollIntoView(dgv.Items[row], dgv.Columns[col]);