Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: How do I set the focus on a datagrid to a specific row?

I would like to set the focus on the first row of a data grid.

This is what I have so far:

Keyboard.Focus(ResultsGrid)
If result.Count > 0 Then
    ResultsGrid.SelectedIndex = 0
End If

This will set the focus to the datagrid, but not the row itself.

like image 391
Jonathan Allen Avatar asked Mar 19 '10 22:03

Jonathan Allen


2 Answers

After selecting the row you will have to set the focus on the row in the following way:

ResultsGrid.SelectedIndex = index;
DataGridRow row = (DataGridRow)ResultsGrid.ItemContainerGenerator.ContainerFromIndex(index);
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
like image 153
Guido Zanon Avatar answered Nov 07 '22 18:11

Guido Zanon


Try this:

yourDataGrid.SelectedItem = yourDataGrid.Items[i];
like image 1
Taylor Leese Avatar answered Nov 07 '22 18:11

Taylor Leese