Define KeyBindings in a UserControl, but do they apply for the entire window?

There are several keyboard shortcuts that I would like to get available for the entire system, regardless of where the focus is now.

I currently have those that are defined as <Window.InputBindings> in the main XAML window. Everything is fine, but ... The ribbon is encapsulated as a separate UserControl, and all the shortcuts, of course, are associated with the buttons that are on this ribbon.
Therefore, I do not really like the smell of having two parts of applied logic dealing with the same functionality spreading over 2 classes.

Ideally, I would like to define KeyBindings as <Window.InputBindings> , but in UserControl, not in the window. Obviously, this cannot be done with the XAML syntax, but only <UserControl.InputBindings> - alas, that will only make them available when the focus is on the tape (this means that never).

Any suggestions?

Thanks Alex

+4
source share
1 answer

In the constructor of your control or in its Loaded () event handler, find the window it is in (use the recursive method VisualTreeHelper.GetParent (this) until GetType is Window ...), then add the InputBindings that you want this window .

+2
source

Source: https://habr.com/ru/post/1383246/


All Articles