Condividi tramite


RoutedCommand.CanExecute(Object, IInputElement) Metodo

Definizione

Determina se questa RoutedCommand operazione può essere eseguita nello stato corrente.

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

Parametri

parameter
Object

Tipo di dati definito dall'utente.

target
IInputElement

Destinazione del comando.

Restituisce

true se il comando può essere eseguito nella destinazione del comando corrente; in caso contrario, false.

Attributi

Eccezioni

Esempio

L'esempio seguente è un CanExecuteChanged gestore eventi di un'implementazione personalizzata di ICommandSource.

this.Command in questo esempio è la Command proprietà dell'oggetto ICommandSource. Se il comando non nullè , viene eseguito il cast del comando a un oggetto RoutedCommand. Se il comando è un RoutedCommand, il CanExecute metodo viene chiamato passando CommandTarget e CommandParameter. Se il comando non è un RoutedCommand, viene eseguito il cast a un ICommand oggetto e il CanExecute metodo viene chiamato passando .CommandParameter

Se il CanExecute metodo restituisce true, il controllo è abilitato; in caso contrario, il controllo viene disabilitato.

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

Commenti

La logica effettiva che determina se un RoutedCommand oggetto può essere eseguito sulla destinazione del comando corrente non è contenuto nei CanExecute metodi , genera invece CanExecute gli PreviewCanExecute eventi che eseguono il tunneling e la bolla attraverso l'albero CanExecute degli elementi che cercano un oggetto con un CommandBindingoggetto . Se viene trovato un CommandBinding oggetto per , RoutedCommand viene chiamato l'oggetto CanExecuteRoutedEventHandler associato a CommandBinding . Questi gestori forniscono la logica di programmazione per determinare se l'oggetto RoutedCommand può essere eseguito o meno.

Gli PreviewCanExecute eventi e PreviewExecuted vengono generati nell'oggetto CommandTarget. Se l'oggetto CommandTarget non è impostato su ICommandSource, gli PreviewCanExecute eventi e CanExecute vengono generati sull'elemento con lo stato attivo della tastiera.

Si applica a