Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为子菜单项创建键盘快捷键_C#_Wpf_Wpf Controls - Fatal编程技术网

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

C# 如何为子菜单项创建键盘快捷键,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,我正在使用wpf。我遇到的一个问题是为菜单项创建快捷键。 如何通过键盘快捷键调用子菜单项。 有人能知道这件事吗?请帮助我..提前谢谢。。 我的.xaml文件是 <MenuItem Header="_Open file" Name="open" IsCheckable="True" Click="file_click" InputGestureText="ctrl+o"> <MenuItem.InputBindings> <KeyBinding

我正在使用wpf。我遇到的一个问题是为菜单项创建快捷键。 如何通过键盘快捷键调用子菜单项。 有人能知道这件事吗?请帮助我..提前谢谢。。 我的.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>

在您的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>

它在command=“open”处生成错误。错误是“无法将属性“command”中的字符串“open”转换为类型为“System.Windows.Input.ICommand”的对象。CommandConverter无法从System.string转换。”
<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();
    }