我是WPF的新手,在我正在构建的应用程序中,我希望在按下alt键时显示主菜单,就像在Vista和Windows 7中的Windows资源管理器一样。我尝试使用键绑定只是修饰符集,但似乎不起作用。
到目前为止,代码是:
<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>
当焦点丢失时,我也希望菜单消失。
有什么想法吗?
答案 0 :(得分:2)
我在寻找的答案可以在这里找到:
感谢大家的帮助。
答案 1 :(得分:2)
免责声明:由于用户已找到解决方案,因此不是尝试答案。这只是为了提供有关该主题的其他信息。
对于任何想知道@ jamier尝试此KeyBinding
的原因无法解决的问题的人,可以在MSDN上的KeyBinding
Class页面找到答案:
除功能键和数字小键盘键外,有效
KeyGesture
必须只包含一个键和一个或多个ModifierKeys
。
因此,一个修饰符本身不能用作Gesture
中的有效KeyBinding
。
答案 2 :(得分:1)
您是否尝试将Key
属性设置为"LeftAlt"
或"RightAlt"
? Key
属性的类型为System.Windows.Input.Key
枚举,其值没有"Alt"
。
Alt
用作KeyGesture
中的修饰符,因此您可以在其他地方单独看到它。但是,在Key枚举中,它特别具有左右Alt键的实例。
您很可能必须拥有两个绑定,每个alt键一个。
答案 3 :(得分:0)
尝试在IsMainMenu="True"
控件上设置Menu
。这会给你你正在寻找的行为吗?
答案 4 :(得分:0)
尝试设置Modifiers="Alt"
和Key="LeftAlt"
:
<KeyBinding Key="LeftAlt" Modifiers="Alt" Command="{x:Static local:MainWindow.ShowMenuCommand}" />