开发者

WPF Commanding problem

开发者 https://www.devze.com 2023-01-28 02:01 出处:网络
Why commanded control is always disabled however command can be executed? Command also runs with Alt + F4

Why commanded control is always disabled however command can be executed? Command also runs with 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 OnShutDownComman开发者_Python百科dCanExecute(object sender, CanExecuteRoutedEventArgs e) {
        e.CanExecute = true;
    }
}

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


Usually this happens because there's no CommandBinding for the command in the scope of the control which has the Command set on it. If you set a breakpoint in the CanExecute handler does it get hit for the MenuItem?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号