如何在WPF中禁止菜单键盘快捷键KeyDown处理? [英] How do I suppress menu keyboard shortcut KeyDown handling in WPF?

查看:218
本文介绍了如何在WPF中禁止菜单键盘快捷键KeyDown处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有菜单键盘快捷键和文本框的应用程序.当文本框具有焦点时,我希望禁用键盘快捷键,但是我不知道这样做的简单方法.我可以处理文本框的PreviewKeyDown事件,但是发送KeyDown事件不会导致TextInput事件触发,因此我必须自己手动触发TextInput事件,并且必须确保每个文本框都覆盖PreviewKeyDown事件并创建一个TextInput事件.

I have an application that has menu keyboard shortcuts and text boxes. I want the keyboard shortcuts to be disabled when the text box has focus, but I can't figure out an easy way to do this. I could handle the text box's PreviewKeyDown event, but sending a KeyDown event doesn't cause the TextInput event to trigger so I'd have to manually trigger the TextInput event myself, and I'd have to make sure that every text box overrides the PreviewKeyDown event and creates a TextInput event.

当文本框具有焦点时,这是抑制菜单键盘快捷键的唯一方法吗?还是存在错误较少的另一种方法?

Is this the only way suppress menu keyboard shortcuts when a textbox has focus or is there another way that is less error prone?

这是我添加键盘快捷键的方式:

Here's how I'm adding the keyboard shortcut:

var kgc = new NuiWpfCore.Input.UnrestrictedKeyGestureConverter();  // allows gestures without modifier keys
var result = kgc.ConvertFromString(s) as NuiWpfCore.Input.UnrestrictedKeyGesture;
m_KeyBinding = new KeyBinding();
m_KeyBinding.Command = KeyBindingCommand;
m_KeyBinding.Modifiers = result.Modifiers;
m_KeyBinding.Key = result.Key;
m_Parent.InputBindings.Add(m_KeyBinding); // m_Parent is of type UIElement

推荐答案

可以像在注册键盘快捷键的方式中那样提供更多输入吗?使用KeyBinding?如果是这样,它已经需要指定Command.因此,如果文本框处于焦点位置,则在Canexecute命令中返回false.

Can you provide more inputs as in HOW are you registering the Keyboard shortcuts? Using KeyBinding? If so it already needs Command specified. So in the Canexecute of the command return false if the textbox is in focus.

这将禁用键盘快捷键.您身边的一些源可能有用.

This will disable keyboard shortcuts. Some source ocde from your side might be useful.

编辑

所以现在您拥有使用KeyBindingCommandKeyBinding,对我而言,这就像一个RoutedCommand.如果是这样,则具有CanExecute功能的命令绑定.

SO now that you have KeyBinding using KeyBindingCommand which look like a RoutedCommand to me. If so do command bindings having CanExecute function.

    m_Parent.CommandBindings.Add(new CommandBinding(KeyBindingCommand, OnExecuted, CanExcute));

CanExecute处理程序中....CanExecutedRoutedArgs 可能/不正确是正确的...

In the CanExecute handler.... CanExecutedRoutedArgs may / may not be correct...

    private void CanExecute(object sender, CanExecutedRoutedArgs args)
    {
          e.CanExecute = !textBox.IsFocused;
    } 

上面的代码仅用于说明.

The code above is only for illustration.

这篇关于如何在WPF中禁止菜单键盘快捷键KeyDown处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆