Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard focus to DataGrid

I have a WPF DataGrid and want to set the focus to the first item so that the user can navigate with the keyboard in the list of items, when the dialogue is opened the first time. With datagrid.Focus ( ); I can set the focus to the DataGrid, but this is apparently not the keyboard focus, because when I press the arrow down key, I cannot navigate in the DataGrid. The focus jump to the textbox "Description" but that is not what I want (see picture).

enter image description here

How can I set the focus and the keyboard focus in a correct way to the DataGrid? Thank for your help.

like image 769
simmeone Avatar asked Dec 12 '13 07:12

simmeone


2 Answers

Ok, I found a solution. This works for me

Keyboard.Focus (GetDataGridCell (dataGridFiles.SelectedCells[0]));

private System.Windows.Controls.DataGridCell GetDataGridCell (System.Windows.Controls.DataGridCellInfo cellInfo)
{
  var cellContent = cellInfo.Column.GetCellContent (cellInfo.Item);

  if (cellContent != null)
    return ((System.Windows.Controls.DataGridCell) cellContent.Parent);

  return (null);
}

Now, I got the right focus and can navigate with keyboard.

like image 55
simmeone Avatar answered Oct 06 '22 00:10

simmeone


Try giving keyboard focus manually using Keyboard.Focus -

Keyboard.Focus(dataGrid);
like image 36
Rohit Vats Avatar answered Oct 06 '22 01:10

Rohit Vats