当 DataGrid 接收键盘焦点时,将焦点放在 SelectedItem 的 DataGridCell 上 [英] Focus on DataGridCell for SelectedItem when DataGrid Receives Keyboard Focus
问题描述
我有一个 DataGrid
,其中 SelectedItem
绑定到 VM Selected 属性.我有一个搜索控件可以进行查找,并且 DataGrid
的 SelectedItem
会发生变化(并滚动到视图中).WPF 4.0 和 DataGrid
SelectionUnit="FullRow"
.
I have a DataGrid
where the SelectedItem
is bound to a VM Selected property. I have a search control that will do a find and the SelectedItem
of the DataGrid
changes (and scrolls into view). WPF 4.0 and DataGrid
SelectionUnit="FullRow"
.
我的问题在于焦点.DataGrid
接收焦点(通过附加属性/绑定),但您不能使用 Up、Down、Page Up、Page Down 键来改变行(SelectedItem
).如果我再次使用 Tab,则会选择显示的第一行的第一个单元格,这会更改 SelectedItem
.
My problem is with the focus. The DataGrid
receives focus (via attached property / binding) but you can't use the Up, Down, Page Up, Page Down keys to change rows (SelectedItem
). If I tab again, the first cell of the first row displayed is selected which changes the SelectedItem
.
底线,当 DataGrid
获得焦点时,如何将键盘焦点赋予 SelectedItem
的 DataGridCell
?
Bottom line, how can I give keyboard focus to the DataGridCell
for the SelectedItem
when the DataGrid
receives focus?
有很多 DataGrid
/Focus
问题,并且已经尝试了一些方法.感谢您的帮助.
There are so many DataGrid
/ Focus
questions and tried a few things already. Thanks for your help.
推荐答案
你需要给新选择的行赋予逻辑焦点.选择新项目后,尝试将您的 SetFocus
调用替换为:
You need to give the newly selected row logical focus. After selecting the new item try replacing your SetFocus
call with this:
var selectedRow = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(dataGrid1.SelectedIndex);
FocusManager.SetIsFocusScope(selectedRow, true);
FocusManager.SetFocusedElement(selectedRow, selectedRow);
这篇关于当 DataGrid 接收键盘焦点时,将焦点放在 SelectedItem 的 DataGridCell 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!