我有一个 WPF DataGrid,我想将焦点设置到第一个项目,以便用户可以在第一次打开对话框时使用键盘在项目列表中导航。用datagrid.Focus();我可以将焦点设置到 DataGrid,但这显然不是键盘焦点,因为当我按下向下箭头键时,我无法在 DataGrid 中导航。焦点跳转到文本框“描述”,但这不是我想要的(见图)。
如何以正确的方式将焦点和键盘焦点设置到 DataGrid?感谢您的帮助。
最佳答案
好的,我找到了解决方案。这对我有用
Keyboard.Focus (GetDataGridCell (dataGridFiles.SelectedCells[0]));
private System.Windows.Controls.DataGridCell GetDataGridCell (System.Windows.Controls.DataGridCellInfo cellInfo)
{
var cellContent = cellInfo.Column.GetCellContent (cellInfo.Item);
if (cellContent != null)
return ((System.Windows.Controls.DataGridCell) cellContent.Parent);
return (null);
}
现在,我获得了正确的焦点并且可以使用键盘进行导航。
关于c# - 键盘焦点到 DataGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20537073/