【问题标题】:WPF Commanding problemWPF命令问题
【发布时间】:2010-12-01 19:37:16
【问题描述】:

为什么命令控制总是被禁用但命令可以执行?命令也与Alt + F4一起运行

public static class CommandLibrary {
    static CommandLibrary() {
        ShutDownCommand = new RoutedUICommand("Exit", "Exit", typeof(CommandLibrary), new InputGestureCollection {new KeyGesture(Key.F4, ModifierKeys.Alt)});
    }

    public static RoutedUICommand ShutDownCommand { get; private set; }

    public static void BindCommands(Window hostWindow) {
        if (hostWindow == null)
            return;

        hostWindow.CommandBindings.Add(new CommandBinding(ShutDownCommand, OnShutDownCommandExecuted, OnShutDownCommandCanExecute));
    }

    private static void OnShutDownCommandExecuted(object sender, ExecutedRoutedEventArgs e) {
        MessageBox.Show("ShutDown Excuted!");
    }

    private static void OnShutDownCommandCanExecute(object sender, CanExecuteRoutedEventArgs e) {
        e.CanExecute = true;
    }
}

<MenuItem Command="local:CommandLibrary.ShutDownCommand" />

【问题讨论】:

    标签: wpf routed-commands


    【解决方案1】:

    通常发生这种情况是因为在设置了 Command 的控件范围内没有针对该命令的 CommandBinding。如果您在 CanExecute 处理程序中设置断点,它是否会命中 MenuItem?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-27
      • 2013-03-09
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 2010-10-02
      • 2021-03-10
      相关资源
      最近更新 更多