RoutedCommand.CanExecute(Object, IInputElement) Metoda

Definicja

Określa, czy można to RoutedCommand wykonać w bieżącym stanie.

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

Parametry

parameter
Object

Typ danych zdefiniowany przez użytkownika.

target
IInputElement

Element docelowy polecenia.

Zwraca

true jeśli polecenie może zostać wykonane na bieżącym obiekcie docelowym polecenia; w przeciwnym razie , false.

Atrybuty

Wyjątki

target nie jest elementem UIElement lub ContentElement.

Przykłady

Poniższy przykład to CanExecuteChanged procedura obsługi zdarzeń z niestandardowej implementacji programu ICommandSource.

this.Command w tym przykładzie Command jest to właściwość w obiekcie ICommandSource. Jeśli polecenie nie nullma wartości , polecenie zostanie oddane do .RoutedCommand Jeśli polecenie jest poleceniem RoutedCommand, CanExecute metoda jest wywoływana, przekazując element CommandTarget i CommandParameter. Jeśli polecenie nie jest elementem RoutedCommand, jest rzutowanie do elementu ICommand , a CanExecute metoda jest wywoływana przekazując element CommandParameter.

CanExecute Jeśli metoda zwraca truewartość , kontrolka jest włączona. W przeciwnym razie kontrolka zostanie wyłączona.

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

Uwagi

Rzeczywista logika określająca, czy obiekt RoutedCommand może zostać wykonany w bieżącym obiekcie docelowym polecenia, nie jest zawarta w CanExecute metodach, a zdarzenia CanExecuteCanExecutePreviewCanExecute, które tunelują i bąbelkowe przez drzewo elementów, wyszukując obiekt z elementem .CommandBinding CommandBinding Jeśli element dla tego RoutedCommand elementu zostanie znaleziony, wywołana CanExecuteRoutedEventHandler jest wartość dołączona doCommandBinding. Te programy obsługi udostępniają logikę programowania na potrzeby określania RoutedCommand , czy można wykonać, czy nie.

Zdarzenia PreviewCanExecute i PreviewExecuted są wywoływane w obiekcie CommandTarget. Jeśli właściwość CommandTarget nie jest ustawiona na ICommandSourceobiekcie , zdarzenia PreviewCanExecute i CanExecute są wywoływane na elemecie z fokusem klawiatury.

Dotyczy