我正在使用wpf.i在为菜单项创建键盘快捷键时遇到了一个问题。如何通过键盘快捷键调用子菜单项。有人知道这件事吗?请提前帮助me..thanks ..我的.xaml文件是
<MenuItem Header="_Open file" Name="open" IsCheckable="True" Click="file_click" InputGestureText="ctrl+o">
<MenuItem.InputBindings>
<KeyBinding Key="O" Modifiers="control"/>
</MenuItem.InputBindings>
</MenuItem>
我在.cs文件中的clickevent是
private void file_click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd;
ofd = new OpenFileDialog();
ofd.AddExtension = true;
ofd.DefaultExt = "*.*";
ofd.Filter = "media (*.*)|*.*";
ofd.ShowDialog();
mediaElement1.Source = new Uri(ofd.FileName);
listBox1.Items.Add(ofd.SafeFileName);
mediaElement1.Play();
}发布于 2012-03-09 17:15:57
在你的XAML中试试这个:
<Window.CommandBindings>
<CommandBinding Command="Open" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Command="Open" Key="O" Modifiers="control" />
</Window.InputBindings>
<Grid>
<MenuItem Header="_Open file" Name="open" IsCheckable="True" Command="Open" InputGestureText="ctrl+o" DockPanel.Dock="Top">
</MenuItem>
</Grid>代码隐藏:
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) {
OpenFileDialog ofd;
ofd = new OpenFileDialog();
ofd.AddExtension = true;
ofd.DefaultExt = "*.*";
ofd.Filter = "media (*.*)|*.*";
ofd.ShowDialog();
}https://stackoverflow.com/questions/9630849
复制相似问题