在 WPF 和 WinForms 控件中处理相同快捷方式的最佳方法是什么?

发布于 2024-09-05 11:18:53 字数 390 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2024-09-12 11:18:53

我很困惑为什么您不使用内置命令:

  • ApplicationCommands.Undo
  • ApplicationCommands.Redo

使用这些内置命令有几个优点:

  1. 它们的键绑定是根据区域设置自动为您设置的(Ctrl + ZCtrl + Y 可能不是所有语言环境中的默认撤消/重做键)
  2. 它们受到 TextBoxRichTextBox 的支持,
  3. 它们跨越 WPF <-> WinForms 边界没有任何问题
  4. 它们使用辅助功能接口
  5. 它们由具有它们的键盘上内置的“撤消”键调用

因此,如果可能的话,您应该通过简单地注册 CommandBindings 来使用内置的 ApplicationCommands 将它们放在代码中适当的位置。

更多信息

如果您在 WPF 和 WinForms 中使用内置的撤消/重做功能,它就可以正常工作。例如,下面创建了两个 RichTextBox,一个基于 WinForms,一个基于 WPF,并且两者都具有完整的撤消/重做功能:

<UniformGrid Columns="2"
  xmlns:winforms=
    "clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">

  <WindowsFormsHost >
    <winforms:RichTextBox />
  </WindowsFormsHost>

  <RichTextBox />

</UniformGrid>

由于此方法有效,而您的无效,请尝试找出不同之处。您在评论中表示您尝试删除自定义 WPF InputBindings。你在 WinForms 端也做过同样的事情吗?如果没有,请尝试一下,或者如果不可能,请编辑您的问题以显示该代码。

请注意,您可以将 ApplicationCommands 重新映射到您自己的 RoatedCommands:只需添加 CommandBinding 并在处理程序中触发您的自定义 RoatedCommand >。

I'm puzzled why you aren't using the built-in commands:

  • ApplicationCommands.Undo, and
  • ApplicationCommands.Redo

There are several advantages to using these built-in commands:

  1. Their key bindings are automatically for you set based on locale (Ctrl + Z and Ctrl + Y may not be the default undo/redo keys in all locales)
  2. They are honored by TextBox and RichTextBox
  3. They cross the WPF <-> WinForms boundary without any problems
  4. They work with accessibility interfaces
  5. They are invoked by built-in "undo" keys on keyboards that have them

So if possible you should use the built in ApplicationCommands by simply registering CommandBindings 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:

<UniformGrid Columns="2"
  xmlns:winforms=
    "clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">

  <WindowsFormsHost >
    <winforms:RichTextBox />
  </WindowsFormsHost>

  <RichTextBox />

</UniformGrid>

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 own RoutedCommands: Just add a CommandBinding and in the handler fire your custom RoutedCommand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文