如何在 WPF/WinForms 中仅关闭 ESC 键上的单个对话框?
我有一个 WPF 表单。它处理 KeyUp
事件,如果释放的键是 Escape,它会自行关闭。
此窗体还有一个按钮,用于将某些 Windows 窗体显示为对话框。它确实以相同的方式处理 Escape 键。
发生的情况是,当我在子对话框中按 Esc 键时,两个窗口都会关闭。我希望在这种情况下只有子 Windows 窗体窗口会关闭。
处理这个问题的正确方法是什么?
I have a WPF form. It handles the KeyUp
event and if the released key was Escape, it closes itself.
Also this form has a button to show some Windows Form as a dialog. It does handle the Escape key in the same way.
What happens is that when I press Escape when in the child dialog, both windows close. I expect only the child Windows Forms window to close in this case.
What's the proper way to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WPF 最简单的选项是将按钮属性
IsCancel
设置为true
。然后,如果您按ESC
,表单将关闭。The easiest option for WPF is set the button property
IsCancel
totrue
. Then if you pressESC
the form would be closed.尝试将 KeyUp 事件的 Handling 属性设置为 true:
Try setting the KeyUp event's handled property equal to true: