RoutedCommand.CanExecute(Object, IInputElement) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Détermine si cela RoutedCommand peut s’exécuter dans son état actuel.
public:
bool CanExecute(System::Object ^ parameter, System::Windows::IInputElement ^ target);
[System.Security.SecurityCritical]
public bool CanExecute(object parameter, System.Windows.IInputElement target);
public bool CanExecute(object parameter, System.Windows.IInputElement target);
[<System.Security.SecurityCritical>]
member this.CanExecute : obj * System.Windows.IInputElement -> bool
member this.CanExecute : obj * System.Windows.IInputElement -> bool
Public Function CanExecute (parameter As Object, target As IInputElement) As Boolean
Paramètres
- parameter
- Object
Type de données défini par l’utilisateur.
- target
- IInputElement
Cible de commande.
Retours
true si la commande peut s’exécuter sur la cible de commande actuelle ; sinon, false.
- Attributs
Exceptions
target n’est pas un UIElement ou ContentElement.
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 un 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 convertie en un ICommand et 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é.
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;
}
}
}
}
Private Sub CanExecuteChanged(ByVal sender As Object, ByVal e As EventArgs)
If Me.Command IsNot Nothing Then
Dim command As RoutedCommand = TryCast(Me.Command, RoutedCommand)
' If a RoutedCommand.
If command IsNot Nothing Then
If command.CanExecute(CommandParameter, CommandTarget) Then
Me.IsEnabled = True
Else
Me.IsEnabled = False
End If
' If a not RoutedCommand.
Else
If Me.Command.CanExecute(CommandParameter) Then
Me.IsEnabled = True
Else
Me.IsEnabled = False
End If
End If
End If
End Sub
Remarques
La logique réelle qui détermine si une RoutedCommand peut s’exécuter sur la cible de commande actuelle n’est pas contenue dans les CanExecute méthodes, déclenche plutôt CanExecute les PreviewCanExecuteCanExecute événements qui tunnelnent et bullent dans l’arborescence d’éléments à la recherche d’un objet avec un CommandBinding. Si un CommandBinding pour cela RoutedCommand est trouvé, le CanExecuteRoutedEventHandler joint à CommandBinding est appelé. Ces gestionnaires fournissent la logique de programmation pour déterminer si la RoutedCommand valeur peut s’exécuter ou non.
Les PreviewCanExecute événements et PreviewExecuted les événements sont déclenchés sur le CommandTarget. Si le CommandTarget paramètre n’est pas défini sur le ICommandSource, les PreviewCanExecute événements et CanExecute les événements sont déclenchés sur l’élément avec le focus clavier.