A
A
Alexey2016-12-02 10:52:20
WPF
Alexey, 2016-12-02 10:52:20

How to finish editing a DataGrid cell with the arrows on the keyboard?

By default, editing a DataGrid cell is terminated when the Enter key, tab key is pressed, or when focus is lost.
But it was necessary to make it so that this would also happen when you press the UP and Down keys (up and down arrows).
By default, when editing the Grid, these keys do not respond.
I feel that the solution should be on the surface, but I don’t really want to reinvent the wheel.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2016-12-02
@k1lex

Try handling the PreviewKeyDown event :

private void dataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
  if (e.Key == Key.Up || e.Key == Key.Down)
  {
    dataGrid.CancelEdit();
  }
}

<DataGrid x:Name="dataGrid" PreviewKeyDown="dataGrid_PreviewKeyDown" ...>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question