按键绑定手势“Ctrl+1"在 RichTextBox 中不起作用 [英] KeyBinding gesture "Ctrl+1" in RichTextBox doesn't work

查看:46
本文介绍了按键绑定手势“Ctrl+1"在 RichTextBox 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WPF 中将命令绑定到 Ctrl+N 手势时有一个奇怪的区别.这些手势中的一些但不是全部被忽略,而其余的则可以.有没有其他人偶然经历过这种行为?

There is a strange difference when binding commands to Ctrl+N gestures in WPF. Some but not all of these gestures are ignored, while the rest are ok. Did anyone else experience this behavior by any chance?

Window XAML 结构非常简单:命令绑定、输入绑定以及带有 Menu 和 RichTextBox 的 DockPanel.经测试,只有输入焦点在RichTextBox时才会出现问题.

Window XAML structure is very simple: command binding, input binding, and a DockPanel with Menu and RichTextBox. After testing, the problem appears only when input focus is in RichTextBox.

为了测试,为所有数字键定义了窗口输入绑定,如下所示.结果,Ctrl+1Ctrl+2Ctrl+5 什么也做不了,而 Ctrl+346-90 工作正常.Alt+1Alt+2 也正常.

For the test, Window input binding was defined for all numeric keys as shown below. As a result, Ctrl+1, Ctrl+2, and Ctrl+5 wouldn't do anything, while Ctrl+3, 4, 6-9, and 0 are working just fine. Alt+1, Alt+2 are working ok as well.

 <Window.InputBindings>
  <KeyBinding Command="me:MainWindow.MyRC" Gesture="CTRL+0" />
  <KeyBinding Command="me:MainWindow.MyRC" Gesture="CTRL+1" />
  ...
  <KeyBinding Command="me:MainWindow.MyRC" Gesture="CTRL+9" />
 </Window.InputBindings>

问题原因

正如 Andy 指出的,RichTextBox 已经将一些手势绑定到内部命令.当在 RTB 内处理此手势时,它不会传递到窗口级别.当按键对 RTB 中的文本没有明显影响时,这有点难以检测.

Problem cause

As Andy pointed out, RichTextBox already binds some gestures to internal commands. When this gesture is handled inside RTB it won't be passed to the Window level. This is somewhat hard to detect when there is no apparent effect of key stroke on the text in RTB.

如果命令仅在 RTB 上下文中有意义 - 移动其中的输入绑定以覆盖默认命令绑定:

If command only makes sense in the context of RTB - move input binding inside it to override default command binding:

<RichTextBox.InputBindings>
    <KeyBinding Command="me:WindowBindingTest.MyRC1" Gesture="CTRL+1" />
    <KeyBinding Command="me:WindowBindingTest.MyRC2" Gesture="CTRL+2" />
    <KeyBinding Command="me:WindowBindingTest.MyRC5" Gesture="CTRL+5" />
</RichTextBox.InputBindings>

解决方案 2

如果命令具有更大的范围"并且即使输入焦点在 RTB 之外也应该可用 - 从未使用的默认 RTB 命令中删除手势绑定:

Solution 2

If commmand has "larger scope" and should be available even when input focus is outside of RTB - remove gesture binding from unused default RTB commands:

<RichTextBox.InputBindings>
    <KeyBinding Command="NotACommand" Gesture="CTRL+1" />
    <KeyBinding Command="NotACommand" Gesture="CTRL+2" />
    <KeyBinding Command="NotACommand" Gesture="CTRL+5" />
</RichTextBox.InputBindings>

推荐答案

看起来 RichTextBox 已经使用 Ctrl+1 定义了命令的绑定(ApplySingleSpace").因此,它可能会处理该命令,而不是让它路由到 Window.

It looks like the RichTextBox already defines a binding for a command using Ctrl+1 ("ApplySingleSpace"). Therefore it is likely handling the command and not letting it route up to the Window.

您可以通过将 InputBinding 添加到 RichTextBox 来解决此问题,但因为您还需要在 Window 上使用它> 因为当其他控件处于焦点时,这不是一个很好的解决方案.

You might be able to work around this by adding an InputBinding to the RichTextBox, but since you'd also need it on the Window for when other controls are in focus, that's not a very good solution.

这篇关于按键绑定手势“Ctrl+1"在 RichTextBox 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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