WPF 用户控件中的键盘输入不会发送到 WinForms 容器
我们有一个 WinForms 应用程序,我们正在逐步将其转换为 WPF。此时,应用程序的主窗体是一个 Form (WinForm),其中包含 WPF 中内置的垂直侧边栏。侧边栏托管在 ElementHost 控件中。
在主窗体中,KeyPreview 设置为 true,我们重写 OnKeyDown() 来处理应用程序范围的键盘快捷键。当侧边栏获得焦点时,键盘事件不会发送到 OnKeyDown。
解决这个问题的正确方法是什么?
We have a WinForms application that we are progressively converting to WPF. At this point the application's main form is a Form (WinForm) that contains a vertical sidebar built in WPF. The sidebar is hosted in an ElementHost control.
In the main form, KeyPreview is set to true and we override OnKeyDown() to process application wide keyboard shortcuts. When the sidebar has the focus, keyboard events are not sent to OnKeyDown.
What is the correct way to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,ElementHost 似乎没有考虑 KeyPreview,这里有一个解决方法:
从 ElementHost 派生并覆盖 ProcessCmdKey,当 base.ProcessCmdKey 结果显示“未处理”时,将消息传递给父级,即使它不是您的主窗体,这样你的主窗体就会收到它,因为其他 winforms 控件将正常运行。这是一个示例...
Yes, it seems the KeyPreview is not consider by ElementHost, here is a workaround:
Derive from ElementHost and override ProcessCmdKey, when the base.ProcessCmdKey result says "not processed", pass the message to the parent even if it is not your main form, this way your main form will receive it because other winforms control will behave correctly. Here is a sample...