0

コマンドを実行できるのに、コマンド制御が常に無効になっているのはなぜですか?コマンドもで実行されます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" />
4

1 に答える 1

1

通常、これは、コマンドが設定されているコントロールのスコープ内にコマンドのCommandBindingがないために発生します。CanExecuteハンドラーにブレークポイントを設定した場合、MenuItemにヒットしますか?

于 2010-12-01T22:09:00.547 に答える