WPF: How do I set the focus on a datagrid to a spe

2019-02-24 06:11发布

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.

2条回答
我只想做你的唯一
2楼-- · 2019-02-24 06:49

Try this:

yourDataGrid.SelectedItem = yourDataGrid.Items[i];
查看更多
我想做一个坏孩纸
3楼-- · 2019-02-24 07:14

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));
查看更多
登录 后发表回答