在 WPF 和 WinForms 控件中处理相同快捷方式的最佳方法是什么?
我有一个 WPF 应用程序,其主窗口上具有以下 KeyBinding:
<KeyBinding Command="Commands:EditCommands.Undo" Gesture="CTRL+Z" />
<KeyBinding Command="Commands:EditCommands.Redo" Gesture="CTRL+Y" />
这使得命令可以很好地响应快捷方式。但是,在所有嵌入 WinForms 文本框或富文本框的地方,我都无法使用这些快捷方式。如果我删除上述绑定,WinForms 快捷方式可以正常工作。
如何在 WinForms 和 WPF 中支持这些快捷方式?我更喜欢通用方法,因为这个问题可能会影响具有相同键绑定的许多其他命令。
I have a WPF application with the following KeyBinding on its main window:
<KeyBinding Command="Commands:EditCommands.Undo" Gesture="CTRL+Z" />
<KeyBinding Command="Commands:EditCommands.Redo" Gesture="CTRL+Y" />
This makes the command respond to the shortcut fine. However, in all the places where I have embedded WinForms text boxes or rich text boxes, I've lost the ability to use those shortcuts. If I remove the above bindings, the WinForms shortcuts work fine.
How can I support these shortcuts in both WinForms and WPF? I'd prefer a generic method since this problem is likely to affect many other commands with the same keybindings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很困惑为什么您不使用内置命令:
ApplicationCommands.Undo
和ApplicationCommands.Redo
使用这些内置命令有几个优点:
TextBox
和RichTextBox
的支持,因此,如果可能的话,您应该通过简单地注册
CommandBindings 来使用内置的
将它们放在代码中适当的位置。ApplicationCommands
更多信息
如果您在 WPF 和 WinForms 中使用内置的撤消/重做功能,它就可以正常工作。例如,下面创建了两个
RichTextBox
,一个基于 WinForms,一个基于 WPF,并且两者都具有完整的撤消/重做功能:由于此方法有效,而您的无效,请尝试找出不同之处。您在评论中表示您尝试删除自定义 WPF
InputBindings
。你在 WinForms 端也做过同样的事情吗?如果没有,请尝试一下,或者如果不可能,请编辑您的问题以显示该代码。请注意,您可以将
ApplicationCommands
重新映射到您自己的RoatedCommands
:只需添加CommandBinding
并在处理程序中触发您的自定义RoatedCommand
>。I'm puzzled why you aren't using the built-in commands:
ApplicationCommands.Undo
, andApplicationCommands.Redo
There are several advantages to using these built-in commands:
TextBox
andRichTextBox
So if possible you should use the built in
ApplicationCommands
by simply registeringCommandBindings
for them at the appropriate places in your code.More information
If you use the built in undo/redo functionality in both WPF and WinForms, it just works. For example, the following creates two
RichTextBoxes
, one based on WinForms and one on WPF, and both have full undo/redo capabilities:Since this works and yours doesn't, try to figure out what is different. You said in your comments you tried removing the custom WPF
InputBindings
. Have you done the same on the WinForms side? If not, please try it, or if that isn't possible please edit your question to show that code as well.Note that you can remap
ApplicationCommands
into your ownRoutedCommands
: Just add aCommandBinding
and in the handler fire your customRoutedCommand
.