2010-12-01 6 views
0

Pourquoi le contrôle commandé est toujours désactivé mais la commande peut être exécutée? Commande fonctionne aussi avec Alt + F4WPF Problème de commande

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" /> 

Répondre

1

Habituellement, cela se produit parce qu'il n'y a pas CommandBinding pour la commande dans le cadre du contrôle qui a le jeu de commandes sur elle. Si vous définissez un point d'arrêt dans le gestionnaire CanExecute, est-il touché par MenuItem?