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

安稳与你 提交于 2019-12-01 19:38:41

问题


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.


回答1:


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));



回答2:


Try this:

yourDataGrid.SelectedItem = yourDataGrid.Items[i];


来源:https://stackoverflow.com/questions/2480850/wpf-how-do-i-set-the-focus-on-a-datagrid-to-a-specific-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!