键盘专注于DataGrid [英] Keyboard focus to DataGrid

查看:150
本文介绍了键盘专注于DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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);
}

现在,我得到了正确的焦点,可以用键盘导航。 >

I have a WPF DataGrid and want to set the focus to the first item so that the user can navigate with the keyboard in the list of items, when the dialogue is opened the first time. With datagrid.Focus ( ); I can set the focus to the DataGrid, but this is apparently not the keyboard focus, because when I press the arrow down key, I cannot navigate in the DataGrid. The focus jump to the textbox "Description" but that is not what I want (see picture).

How can I set the focus and the keyboard focus in a correct way to the DataGrid? Thank for your help.

解决方案

Ok, I found a solution. This works for me

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

Now, I got the right focus and can navigate with keyboard.

这篇关于键盘专注于DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆