如何解决WPF数据网格和Tab键
另一个数据网格键绑定问题 我有一个数据网格。它的选择模式设置为FullRow和KeyboardNavigation.TabNavigation = \“ Once \”,我希望可以得到我想要的结果,但是没有。 当数据网格具有焦点时,如果按下Tab键,则将逐格地在网格的每一列上进行切换。因此,如果我使用Tab键进入具有4列的网格,则必须按Tab 4次才能转到下一个TabIndex。 我想要的是让Tab键在第一次按下时就从数据网格中跳出,然后将焦点放在下一个tabindex上……如果这是有意义的话。 我试图像这样在keydown事件处理程序中覆盖Tab键。class BetterDataGrid : DataGrid
{
..............
protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
{
..............
if (e.Key == Key.Tab)
{
Console.WriteLine(\"TAB\");
MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
.........
}
确实会将\“ TAB \”写入控制台,但选项卡仍保留其默认行为。不知道这是否是进入下一个tabindex的正确方法,但是这应该使tab键除了写到控制台或引起异常外什么都不做。
让我认为无法覆盖Tab键的行为。
希望能提供一些有用的意见。
一如既往,在此先感谢。
解决方法
我希望将其用于我的业务软件,而找到解决该问题的唯一方法是使用datagrid的PreviewKeyDown,GotKeyboardFocus和LostKeyboardFocus事件进行代码隐藏。我将这些事件处理程序放在WPF装饰器中,以避免对每个DataGrid重复进行处理。可能可以对DataGrid进行子类化,但是我还没有尝试过。
处理程序的代码如下(此示例代码的DataGrid为x:Name = \“ grid \”):
private IInputElement lastDataGridFocus = null;
private int selectedcolumnindex = 0;
void grid_GotKeyboardFocus(object sender,KeyboardFocusChangedEventArgs e)
{
if (grid.Items.Count > 0 && (e.NewFocus is DataGrid || (e.NewFocus is DataGridCell && !(e.OldFocus is DataGridCell))))
{
DataGridCell cell = null;
if (lastDataGridFocus != null)
{
FocusManager.SetFocusedElement(grid,lastDataGridFocus);
lastDataGridFocus = null;
e.Handled = true;
return;
}
if (grid.SelectedCells.Count == 0)
{
DataGridRow rowContainer = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(0);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex((selectedcolumnindex < 0) ? 0 : selectedcolumnindex);
}
}
else
{
DataGridCellInfo selectedDataGridCellInfo = (grid.SelectedCells[0] as DataGridCellInfo?).Value;
DataGridRow rowContainer = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(selectedDataGridCellInfo.Item);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex((selectedcolumnindex < 0) ? 0 : selectedcolumnindex);
}
}
if (null != cell)
{
FocusManager.SetFocusedElement(grid,cell as IInputElement);
e.Handled = true;
}
}
}
void grid_LostKeyboardFocus(object sender,KeyboardFocusChangedEventArgs e)
{
if (!(e.NewFocus is DataGridCell))
{
if (grid.CurrentCell != null)
{
selectedcolumnindex = grid.Columns.IndexOf(grid.CurrentCell.Column);
}
}
}
void grid_PreviewKeyDown(object sender,KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Shift && e.Key == Key.Tab)
{
lastDataGridFocus = Keyboard.FocusedElement;
grid.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous));
e.Handled = true;
}
else if (Keyboard.Modifiers == ModifierKeys.None && e.Key == Key.Tab)
{
lastDataGridFocus = Keyboard.FocusedElement;
grid.MoveFocus(new TraversalRequest(FocusNavigationDirection.Last));
(Keyboard.FocusedElement as FrameworkElement).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
e.Handled = true;
}
}
使用此代码,您可以使用光标键在网格内导航,使用Tab键和shift-tab键可以使您脱离数据网格。如果从网格中跳出并返回到网格,则还可以到达剩下的相同单元格。这是我的用户和我想要的,这是恕我直言,DataGrid控件应将其作为默认行为提供。
,您试图达到的目标不是正确的行为,每个人都希望在DataGrid处于焦点状态时按下Tab键时,Excel就像导航一样。如果您不希望用户在DataGrid中导航,最好通过在DataGrid上设置ѭ2来防止Tab键在DataGrid上停止。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。