1

当我尝试使用文本框时,我的应用程序中的 KeyBindings 正在窃取按键消息。例如:

<ribbon:RibbonWindow.InputBindings>
    <KeyBinding Command="{Binding Review.ReviewReviewedCommand}" CommandParameter="Key" Key="Space" />
    <KeyBinding Command="{Binding Review.ReviewLabelPrivilegedCommand}" CommandParameter="Key" Key="P" />
    <KeyBinding Command="{Binding Review.ReviewLabelRelevantCommand}" CommandParameter="Key" Key="R" />
    <KeyBinding Command="{Binding Review.ReviewLabelIrrelevantCommand}" CommandParameter="Key" Key="I" />
    <KeyBinding Command="{Binding Review.ReviewUnassignDocTypeCommand}" CommandParameter="Key" Key="U" />
</ribbon:RibbonWindow.InputBindings>

使用的命令是带有 ICommand 接口的 DelegateCommands。

问题是 Keys P,R,I,U 不能传播到任何文本框。

有没有办法继续路由?

4

1 回答 1

0

只要你使用KeyBinding它就不会没有重大黑客攻击。我为此实施的解决方案是:

  1. 使用该KeyDown事件来捕获那些被按下的键(而不是KeyBindings)。这将在您的代码隐藏中,从那里您需要打开按下的键以调用所需的DataContext's命令(ReviewReviewedCommandReviewLabelPrivilegedCommand等)。
  2. 现在你有一个不同的问题。正在获取输入,TextBox但您的键绑定命令也在触发。在后面的代码中,检查类型keyEventArgs.InputSource并忽略击键,如果它是TextBox.

它应该如下所示:

private void OnKeyDown(object sender, KeyEventArgs e)
{
    ICommand command = null;

    switch (e.Key)
    {
        case Key.Space:
            command = ((YourDataContextType)DataContext).ReviewReviewedCommand;
            break;
        case Key.P:
            command = ((YourDataContextType)DataContext).ReviewLabelPrivilegedCommand;
            break;
    }

    bool isSourceATextBox = e.InputSource.GetType() == typeof(TextBox);
    if (command != null && !isSourceATextBox)
    {
        command.Execute(parameter:null);
    }
}
于 2020-01-09T14:41:27.513 回答