【问题标题】:CommandBinding Ctrl + SpacebarCommandBinding Ctrl + 空格键
【发布时间】:2010-11-15 19:05:08
【问题描述】:

我在实现 RoutedUICommand 的 RoutedCommand 类中处理命令。如果需要,这将帮助我通过检查他们的 CanExecute 和 Execute 来阻止或覆盖命令。我可以覆盖 EditingCommand、ApplicationCommand 等。我什至无法处理的命令之一是 Ctr + Spacebar。它是 MediaCommand 还是我找不到的其他类型?我猜它是在其他地方处理的,所以我无法控制它。

【问题讨论】:

    标签: c# wpf events event-handling commandbinding


    【解决方案1】:

    您可以创建自己的自定义命令,也可以简单地为预定义命令添加新手势,例如:

    public Window1()
        {
            InitializeComponent();
            ApplicationCommands.Find.InputGestures.Add(new KeyGesture(Key.Space, ModifierKeys.Control));
            CommandBinding commandBinding = new CommandBinding(ApplicationCommands.Find, myCommandHandler);
            this.CommandBindings.Add(commandBinding);
        }
    
        private void myCommandHandler(object sender, ExecutedRoutedEventArgs args)
        {
            MessageBox.Show("Command invoked!");
        }
    

    【讨论】:

    • 您的解决方案有效。但是我的问题是当我播放媒体播放器时,后来我发现控件处理的级别更高。
    【解决方案2】:

    我没有太多使用 WPF 命令的经验,但请尝试为 Ctrl 和空格键创建您自己的自定义命令。

    查看本教程:http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 2015-06-03
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多