【问题标题】:How to create keyboard shortcuts for sub menu items如何为子菜单项创建键盘快捷键
【发布时间】:2012-03-09 08:17:30
【问题描述】:

我正在使用 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>

我在 .cs 文件中的点击事件是

   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();



    }

【问题讨论】:

    标签: c# wpf wpf-controls


    【解决方案1】:

    在你的 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();
        }
    

    【讨论】:

    • 它在 command="open" 处生成错误。错误是“无法将属性 'Command' 中的字符串 'open' 转换为 'System.Windows.Input.ICommand' 类型的对象。CommandConverter 无法转换来自 System.String。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2015-06-24
    • 2021-12-18
    相关资源
    最近更新 更多