在 UserControl 中定义 KeyBindings,但它们是否适用于整个窗口?
我希望在应用程序范围内可以使用许多键盘快捷键,无论当前焦点在哪里。
目前,我已在主窗口的 XAML 中将其定义为
。 一切都很好,但是...
功能区被封装为单独的用户控件,并且所有快捷键自然都与该功能区中的按钮相关。
因此,我真的不喜欢让 2 个应用程序逻辑处理分布在 2 个类中的相同功能。
理想情况下,我想将 KeyBindings 定义为
,但在 UserControl 中,而不是在窗口中。显然,这不能使用 XAML 语法来完成,而只能使用
- 唉,这将使它们仅在焦点位于功能区时才可用(这意味着永远不会) em>)。
有什么建议吗?
谢谢, 亚历克斯
There is a number of keyboard shortcuts that I'd like to be available application-wide, regardless of where the focus currently is.
At the moment I've got those defined as <Window.InputBindings>
in my Main window's XAML.
All is good, but...
The Ribbon is encapsulated as a separate UserControl, and all the shortcuts are, naturally, related to the buttons that are in that Ribbon.
Consequently, I don't really like the smell of having 2 pieces of application logic dealing with the same functionality spread across the 2 classes.
Ideally, I would like to define the KeyBindings as <Window.InputBindings>
, but in the UserControl, not in the window. Obviously, that cannot be done with XAML syntax, but only <UserControl.InputBindings>
- alas, that would make them available only when the focus is on the Ribbon (which means never).
Any suggestions?
Thanks,
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在控件的构造函数或其 Loaded() 事件处理程序中,让它找到它所在的窗口(递归使用 VisualTreeHelper.GetParent(this) 直到 GetType 为 Window...),然后将所需的 InputBindings 添加到该窗口窗户。
In the constructor of your control or in its Loaded() event handler, have it find the window in which it stands (use VisualTreeHelper.GetParent(this) recursively until GetType is Window...), then add the InputBindings you want to that window.