WPF: How to set focus on datagrid for a specific row?

I would like to focus on the first row of the 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 focus on the datagrid, but not on the row itself.

+4
source share
2 answers

After selecting a row, you will need to set focus on the row as follows:

 ResultsGrid.SelectedIndex = index; DataGridRow row = (DataGridRow)ResultsGrid.ItemContainerGenerator.ContainerFromIndex(index); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); 
+9
source

Try the following:

 yourDataGrid.SelectedItem = yourDataGrid.Items[i]; 
0
source

Source: https://habr.com/ru/post/1304616/


All Articles