Lire en anglais

Partager via


RoutedCommand.CanExecute(Object, IInputElement) Méthode

Définition

Détermine si ce RoutedCommand peut s'exécuter dans son état actuel.

C#
[System.Security.SecurityCritical]
public bool CanExecute(object parameter, System.Windows.IInputElement target);
C#
public bool CanExecute(object parameter, System.Windows.IInputElement target);

Paramètres

parameter
Object

Type de données défini par l'utilisateur.

target
IInputElement

Cible de la commande.

Retours

true si la commande peut s'exécuter sur la cible de commande actuelle ; sinon false.

Attributs

Exceptions

Exemples

L’exemple suivant est un gestionnaire d’événements CanExecuteChanged à partir d’une implémentation personnalisée de ICommandSource.

this.Command dans cet exemple est la Command propriété sur le ICommandSource. Si la commande n’est pas null, la commande est castée en .RoutedCommand Si la commande est un RoutedCommand, la CanExecute méthode est appelée en passant le CommandTarget et le CommandParameter. Si la commande n’est pas un RoutedCommand, elle est castée en et ICommand la CanExecute méthode est appelée en passant le CommandParameter.

Si la CanExecute méthode retourne true, le contrôle est activé ; sinon, le contrôle est désactivé.

C#
private void CanExecuteChanged(object sender, EventArgs e)
{

    if (this.Command != null)
    {
        RoutedCommand command = this.Command as RoutedCommand;

        // If a RoutedCommand.
        if (command != null)
        {
            if (command.CanExecute(CommandParameter, CommandTarget))
            {
                this.IsEnabled = true;
            }
            else
            {
                this.IsEnabled = false;
            }
        }
        // If a not RoutedCommand.
        else
        {
            if (Command.CanExecute(CommandParameter))
            {
                this.IsEnabled = true;
            }
            else
            {
                this.IsEnabled = false;
            }
        }
    }
}

Remarques

La logique réelle qui détermine si un RoutedCommand peut s’exécuter sur la cible de commande actuelle n’est pas contenue dans les CanExecute méthodes, mais déclenche plutôt CanExecute les PreviewCanExecute événements et qui tunnel et bulle dans l’arborescence d’éléments à la CanExecute recherche d’un objet avec un CommandBinding. Si un CommandBinding pour est RoutedCommand trouvé, le CanExecuteRoutedEventHandler joint à CommandBinding est appelé. Ces gestionnaires fournissent la logique de programmation pour déterminer si le peut s’exécuter RoutedCommand ou non.

Les PreviewCanExecute événements et PreviewExecuted sont déclenchés sur .CommandTarget Si le CommandTarget n’est pas défini sur , ICommandSourceles PreviewCanExecute événements et CanExecute sont déclenchés sur l’élément avec le focus clavier.

S’applique à

Produit Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10