键绑定手势“Ctrl”1在 RichTextBox 中不起作用
在 WPF 中将命令绑定到 Ctrl+N 手势时存在奇怪的差异。这些手势中的一些(但不是全部)会被忽略,而其余的则可以。还有其他人偶然经历过这种行为吗?
Window XAML 结构非常简单:命令绑定、输入绑定以及带有 Menu 和 RichTextBox 的 DockPanel。经测试,仅当输入焦点位于RichTextBox时才会出现该问题。
对于测试,为所有数字键定义了窗口输入绑定,如下所示。 因此,Ctrl+1、Ctrl+2 和 Ctrl+5 不会执行任何操作,而 Ctrl+3 、4、6-9 和 0 工作正常。 Alt+1、Alt+2 也可以正常工作。
<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 中的文本没有明显影响时,这有点难以检测。
解决方案 1
如果命令仅在 RTB 上下文中有意义 - 将输入绑定移至其中以覆盖默认命令绑定:
<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 命令:
<RichTextBox.InputBindings>
<KeyBinding Command="NotACommand" Gesture="CTRL+1" />
<KeyBinding Command="NotACommand" Gesture="CTRL+2" />
<KeyBinding Command="NotACommand" Gesture="CTRL+5" />
</RichTextBox.InputBindings>
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 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.
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>
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.
Solution 1
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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来
RichTextBox
已经使用 Ctrl+1 (“ApplySingleSpace”)定义了命令的绑定。因此,它可能会处理该命令而不让它路由到窗口。您也许可以通过向
RichTextBox
添加InputBinding
来解决此问题,但因为您还需要在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.You might be able to work around this by adding an
InputBinding
to theRichTextBox
, but since you'd also need it on theWindow
for when other controls are in focus, that's not a very good solution.