English 中文(简体)
Keybinding in WPF
原标题:

I m new to WPF and in an application i m building I d like to show the main menu when the alt key is pressed just like windows explorer in vista and windows 7. I ve tried using a keybinding with just the modifier set but that doesn t seem to work.

Heres by code so far:

<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 d also like the menu to disappear when the focus is lost.

Any ideas?

问题回答

The answer I was looking for can be found here:

http://www.stackoverflow.com/questions/1218394/how-can-i-toggle-the-main-menu-visibility-using-the-alt-key-in-wpf

Thanks to everyone for the help.

Disclaimer: This is not an attempt at an answer as the user has already found a solution. This is just to provide additional information on the subject.

For anyone that wants to know why @jamier s attempt at this KeyBinding doesn t work, the answer can be found in the KeyBinding Class page on MSDN:

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

Therefore, one modifier on its own cannot be used as a valid Gesture in a KeyBinding.

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

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

You will more than likely have to have two bindings, one for each alt key.

Try setting IsMainMenu="True" on your Menu control. Does that give you the behavior you re looking for?

Try to set Modifiers="Alt" and Key="LeftAlt":

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签