WPF:如何将数据网格上的焦点设置到特定行?

发布于 2024-08-25 10:53:09 字数 199 浏览 2 评论 0原文

我想将焦点设置在数据网格的第一行。

这就是我到目前为止所拥有的:

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

这会将焦点设置到数据网格,而不是行本身。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水般的淡然安静女子 2024-09-01 10:53:09

选择行后,您必须按以下方式将焦点设置在该行上:

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

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));
猥︴琐丶欲为 2024-09-01 10:53:09

试试这个:

yourDataGrid.SelectedItem = yourDataGrid.Items[i];

Try this:

yourDataGrid.SelectedItem = yourDataGrid.Items[i];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文