如:
<MenuItem Header="hi" InputGestureText="Ctrl+E" Click="MenuItem_Click" />        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Testing");
        }运行时按下Ctrl+E不能弹出对话框,点击可以跳出,请问是哪里出错了?

解决方案 »

  1.   

    此属性没有将输入笔势与菜单项相关联;它只是向菜单项中添加了文本。应用程序必须处理用户的输入才能执行该操作。有关如何将命令与菜单项关联的信息,请参见 Command。你需要通过绑定命令的方式处理快捷键操作,可参考http://srndolha.wordpress.com/2008/03/06/setting-inputgesturetext-on-a-menuitem-bound-to-a-command-in-wpf/
      

  2.   

    现在解决了,另外问一下:为什么加了Command就变成灰色的了?
      

  3.   


    你自己写的Command还是用系统的Command?看看Command的CanExecute方法里有什么限制条件,如果CanExecute返回false,则按钮会不可点击
      

  4.   

            public MainWindow()
            {
                InitializeComponent();
                CommandBindings.Add(new CommandBinding(CustomCommands.ExitCommand, MenuItemClick, CanExecute));
            }        public void CanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = true;
            }这样子写是灰色的
      

  5.   

    给MenuItem定义个名称,然后试一下下面方法行不行menuItem.Add(new CommandBinding(CustomCommands.ExitCommand, MenuItemClick, CanExecute));
      

  6.   


    写错了,应该这样
    menuItem.CommandBindings.Add(new CommandBinding(CustomCommands.ExitCommand, MenuItemClick, CanExecute));
      

  7.   

    或者,你不用CustomCommands.ExitCommand,自己写一个简单的Command测试下Command是否可用
            public static RoutedCommand ExitCommand= new RoutedCommand();
            public Window1()
            {
                InitializeComponent();
            }        // ExecutedRoutedEventHandler for the custom color command.
            private void ExitCommandExecuted(object sender, ExecutedRoutedEventArgs e)
            {
                MessageBox.Show(e.OriginalSource.ToString());
            }        // CanExecuteRoutedEventHandler for the custom color command.
            private void ExitCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                  e.CanExecute = true;        }XAML
    <Window...
    xmlns:custom="clr-namespace:Sample"/>
    ...
    ...
    <Window.CommandBindings>
        <CommandBinding Command="{x:Static custom:Window1.ExitCommand}"
                        Executed="ExitCommandExecuted"
                        CanExecute="ExitCommandCanExecute"/>
      </Window.CommandBindings>
      ...
       ...
       <MenuItem Header="Color Command"
                      Command="{x:Static custom:Window1.ExitCommand}" />
          </MenuItem>