全局 PreviewKeyDown 处理程序与本地 PreviewKeyDown 处理程序

发布于 2024-08-05 14:05:12 字数 265 浏览 4 评论 0原文

我的主窗口上有一个 PreviewKeyDown 处理程序,它可以处理向上和向下键,以便我可以使用键盘在控件之间导航。

现在我遇到的问题是,在某些文本框中我也想使用向上/向下键。这似乎是不可能的,因为另一个处理者似乎先吞下了钥匙。

是否有可能当这些 TextBox 控件之一获得焦点时,它们首先获取向上/向下键,然后吞下它们,以便“全局”PreviewKeyDown 无法获取它们?

当然,当这样的文本框获得焦点时,我可以以某种方式禁用全局处理程序,但这是好的风格吗?

I have a PreviewKeyDown handler on my mainwindow which handles up and down keys so I can navigate with the keyboard between my controls.

Now I have the problem that in some Textboxes I also want to use the up/down keys. This seems impossible because the other handler seems to swallow the keys first.

Is it possible that when one of these TextBox controls are focused they get the up/down keys first and then then swallow them so that the "global" PreviewKeyDown does not get them?

Sure I could disable the global handler somehow when such a TextBox got focus but is this good style?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

抠脚大汉 2024-08-12 14:05:12

除了在全局密钥处理程序中过滤掉这些密钥之外,您实际上没有其他选择。

您遇到此问题的原因是所有 Preview* 事件都是隧道,这意味着可视化树中较高的控件首先获取它们(从根开始)。您首先使用此事件的原因正是导致您出现问题的原因。

一种不太理想的选择是为 TextBox.PreviewKeyDown 注册一个类处理程序(请参阅EventManager.RegisterClassHandler())。虽然这将在窗口的 PreviewKeyDown 处理程序之前调用,但将为应用程序中的所有 TextBox 调用它。这可能是也可能不是您想要的。

You don't really have an option, aside from filtering out those keys in the global key handler.

The reason that you're having this problem is that all of the Preview* events are tunneling, meaning that controls higher in the visual tree get them first (starting at the root). The very reason why you're using this event in the first place is causing your problem.

One less than ideal option would be to register a class handler for TextBox.PreviewKeyDown (see EventManager.RegisterClassHandler()). While this would be called before your window's PreviewKeyDown handler, it will be called for all TextBoxes in your application. This may or not be what you want.

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