【问题标题】:WPF Telerik RadMenuItem Not Firing On HotKeyWPF Telerik RadMenuItem 未在 HotKey 上触发
【发布时间】:2017-11-03 19:42:35
【问题描述】:

下面的 sn-p 来自一个生产机械车间应用程序,它有一个相当实用的菜单.... 我被要求为一些菜单项添加热键;例如,其中一个菜单项(如下面的代码所示)我需要将 F6 键添加为热键(注意单击 vs 热键的相同命令)...单击它可以按需要工作,但热键有似乎没有任何效果(什么都没有发生)。

我错过了什么,或者做错了什么?提前感谢您的帮助。

    <telerik:RadMenuItem Name="ToolsMenu" Header="Tools">
        <telerik:RadMenuItem Name="ToolsUseFinishLocationMenuItem" Header="Use Finish Location For Bin" Command="{Binding ToolsUseFinishLocationForBinCommand}" InputGestureText="F6">
            <telerik:RadMenuItem.InputBindings>
                <KeyBinding Key="F6" Command="{Binding ToolsUseFinishLocationForBinCommand}" />
            </telerik:RadMenuItem.InputBindings>
        </telerik:RadMenuItem>
    </telerik:RadMenuItem>

【问题讨论】:

    标签: c# wpf mvvm telerik


    【解决方案1】:

    可能是因为您没有将键盘焦点放在元素上。您可以考虑将输入绑定到父元素,例如

    <Window>
        <Window.InputBinding>
            <KeyBinding ...>
        </Window.InputBinding
        <your element/>
    </Window>
    

    但也许问题出在其他地方。您还可以使用 InputManager 类为整个应用创建热键。

    将这些行放到构造函数中:

    InputManager.Current.PostNotifyInput -= PostNotifyInputHandler;
    InputManager.Current.PostNotifyInput += PostNotifyInputHandler;
    

    并制作了这样的方法:

    private void PostNotifyInputHandler(object sender, NotifyInputEventArgs args)
        {
            var keyboardArgs = args?.StagingItem?.Input as KeyboardEventArgs;
    
            var isSecondEvent = keyboardArgs?.RoutedEvent != Keyboard.PreviewKeyDownEvent;
            var performedOnTextBox = keyboardArgs?.OriginalSource is System.Windows.Controls.TextBox;
    
            if (isSecondEvent || performedOnTextBox)
            {
                return;
            }
    
            switch ((keyboardArgs as KeyEventArgs)?.Key)
            {
                case Key.F6:
                    // your command here
                    break;
            }
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 2021-03-12
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      相关资源
      最近更新 更多