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

有帮助吗?

解决方案

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

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!");
    }

其他提示

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

请参阅本教程: http:// www .switchonthecode.com /教程/ WPF的教程命令绑定和 - 定制命令

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top