简体   繁体   English

如何为子菜单项创建键盘快捷键

[英]How to create keyboard shortcuts for sub menu items

i am working with wpf.i got a problem is in creating keyboard shortcuts for menu items. 我正在使用wpf。我有一个问题是在为菜单项创建键盘快捷方式。 how to call sub menu item by keyboard shortcuts. 如何通过键盘快捷键调用子菜单项。 can anyone have idea on this?plz help me..thanks in advance.. my .xaml file is 任何人都对此有想法吗?请事先帮助我。谢谢我的.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>

my clickevent in .cs file is 我在.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();



    }

Try this in your XAML: 在您的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>

Code behind: 后面的代码:

 private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) {
        OpenFileDialog ofd;
        ofd = new OpenFileDialog();
        ofd.AddExtension = true;
        ofd.DefaultExt = "*.*";
        ofd.Filter = "media (*.*)|*.*";
        ofd.ShowDialog();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM