WPF指挥问题

时间:2010-12-01 19:37:16

标签: wpf routed-commands

为什么命令控制总是被禁用但是命令可以被执行?命令也与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" />

1 个答案:

答案 0 :(得分:1)

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