由于您的目标是映射箭头键来执行某些键盘选项卡导航,因此您应该将相应的命令映射到键并实施它们。 ComponentCommands.MoveFocusForward
和ComponentCommands.MoveFocusBack
命令在这里是合适的,因为这正是我们正在做的,将焦点转移到下一个或回到先前的控制。
下面是一个例子,你可以做到这一切。
首先,您需要将命令绑定到您的密钥。
<Window.CommandBindings>
<CommandBinding Command="ComponentCommands.MoveFocusForward" Executed="MoveFocusForward_Executed" />
<CommandBinding Command="ComponentCommands.MoveFocusBack" Executed="MoveFocusBack_Executed" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Command="ComponentCommands.MoveFocusForward" Key="Right" />
<KeyBinding Command="ComponentCommands.MoveFocusBack" Key="Left" />
</Window.InputBindings>
然后执行处理程序。
private static bool RequestFocusChange(FocusNavigationDirection direction)
{
var focused = Keyboard.FocusedElement as UIElement;
if (focused != null)
{
return focused.MoveFocus(new TraversalRequest(direction));
}
return false;
}
private void MoveFocusForward_Executed(object target, ExecutedRoutedEventArgs e)
{
RequestFocusChange(FocusNavigationDirection.Next);
}
private void MoveFocusBack_Executed(object target, ExecutedRoutedEventArgs e)
{
RequestFocusChange(FocusNavigationDirection.Previous);
}
不知道是否可以更改事件以指示不同的键;但您应该能够将事件标记为“处理”,然后使用重新映射的关键点创建一个新事件。 – EtherDragon
是的,这就是我上面所说的,但我担心的是,通过将其标记为“已处理”,我可能会失去其他内容,例如发布,重复等。猜测是时候让测试应用程序了! – MarqueIV
与其尝试重新映射键,为什么不将键绑定到与“Tab”和“Shift-Tab”组合绑定的任何命令? –