2010-12-01 66 views
0

爲什麼命令控制總是被禁用,但命令可以執行?命令也運行與Alt + F4WPF指令問題

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

通常出現這種情況,因爲有在其上有命令設定的控制範圍的命令中沒有的CommandBinding。如果您在CanExecute處理程序中設置斷點,它是否會觸發MenuItem?