我刚接触WPF应用程序,现在正在开发一个菜单。我已经创建了菜单,但是我想要将菜单项的事件与快捷键进行绑定,如ctrl+o、ctrl+n等。请告诉我具体如何做。
我刚接触WPF应用程序,现在正在开发一个菜单。我已经创建了菜单,但是我想要将菜单项的事件与快捷键进行绑定,如ctrl+o、ctrl+n等。请告诉我具体如何做。
您可以按以下方式完成...
<Window x:Class="FocusDemo.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FocusDemo"
Title="Window1" Height="300" Width="300">
<Window.CommandBindings>
<CommandBinding
Command=
"{x:Static
local:Window1.CustomRoutedCommand}"
Executed="ExecutedCustomCommand"
CanExecute="CanExecuteCustomCommand" >
</CommandBinding>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding
Command=
"{x:Static
local:Window1.CustomRoutedCommand}"
Key="S"
Modifiers="Control"/>
</Window.InputBindings>
<Grid>
<!--Your Controls-->
</Grid>
</Window>
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public static RoutedCommand CustomRoutedCommand = new RoutedCommand();
public Window1()
{
InitializeComponent();
}
#region
public void ExecutedCustomCommand(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Ctrl+S");
}
public void CanExecuteCustomCommand(object sender,
CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
#endregion
}
来源: 点击这里
如果答案正确请不要忘记标记
我知道这不是问题的确切答案,但可能像我一样的人正在寻找将任何键盘快捷方式放置到菜单项(命令按钮)中的方法,例如Alt+O、Alt+N。在这种情况下,您只需在项目名称中的快捷字符前面放置下划线字符(_)即可。