Hardcover in WPF

I am new to WPF, and in the application that I am creating, I would like to show the main menu when the alt key is pressed, just like Windows Explorer in Vista and Windows 7. I tried using keybinding with just a bunch of modifiers, but that doesn't work.

Shows the code:

<Window.CommandBindings> <CommandBinding Command="{x:Static local:MainWindow.ShowMenuCommand}" CanExecute="ShowMenuCommand_CanExecute" Executed="ShowMenuCommand_Executed"/> </Window.CommandBindings> <Window.InputBindings> <KeyBinding Key="Alt" Command="{x:Static local:MainWindow.ShowMenuCommand}" /> </Window.InputBindings> 

I would also like the menu to disappear when the focus has been lost.

Any ideas?

+4
source share
5 answers
+2
source

Disclaimer: This is not an attempt to reply, as the user has already found a solution. This is just additional information on this.

For those who want to know why @jamier's attempt on this KeyBinding does not work, the answer can be found in the KeyBinding Class on MSDN:

With the exception of function keys and numeric keypad keys, a valid KeyGesture must contain exactly one key and one or more ModifierKeys .

Therefore, a single modifier alone cannot be used as a valid Gesture in KeyBinding .

+2
source

Have you tried to set the Key attribute to "LeftAlt" or "RightAlt" ? The Key attribute is of type System.Windows.Input.Key enumeration, which does not have an "Alt" value.

Alt used as a modifier in KeyGesture , so you see it separately in other places. However, in the key enumeration, it has instances for the left and right Alt keys.

You most likely should have two bindings, one for each alt key.

+1
source

Try setting IsMainMenu="True" to your Menu control. Does this give you the behavior you are looking for?

0
source

Try setting Modifiers="Alt" and Key="LeftAlt" :

 <KeyBinding Key="LeftAlt" Modifiers="Alt" Command="{x:Static local:MainWindow.ShowMenuCommand}" /> 
0
source

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


All Articles