CommandBinding.CanExecute Zdarzenie

Definicja

Występuje, gdy polecenie skojarzone z tym CommandBinding inicjuje sprawdzenie, czy polecenie można wykonać na obiekcie docelowym polecenia.

C#
public event System.Windows.Input.CanExecuteRoutedEventHandler CanExecute;

Typ zdarzenia

Przykłady

Poniższy przykład tworzy obiekt CommandBinding , który mapuje ExecutedRoutedEventHandler program obsługi i CanExecuteRoutedEventArgs na Open polecenie .

XAML
<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>
C#
// Creating CommandBinding and attaching an Executed and CanExecute handler
CommandBinding OpenCmdBinding = new CommandBinding(
    ApplicationCommands.Open,
    OpenCmdExecuted,
    OpenCmdCanExecute);

this.CommandBindings.Add(OpenCmdBinding);

Poniżej przedstawiono, CanExecuteRoutedEventHandler które ustawia wartość CanExecutetrue.

C#
void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}

Uwagi

CanExecute Gdy metoda w obiekcie RoutedCommand jest wywoływana, PreviewCanExecute zdarzenie jest wywoływane w obiekcie docelowym polecenia. Jeśli zdarzenie nie jest obsługiwane, CanExecute zdarzenie jest zgłaszane. Jeśli element docelowy polecenia ma CommandBinding dla polecenia , wywoływana CanExecute jest procedura obsługi dla tego CommandBinding polecenia. Jeśli element docelowy polecenia nie ma CommandBinding elementu docelowego dla polecenia, CanExecute zdarzenie bąbelkuje drzewo elementów wyszukuje element, który ma CommandBinding skojarzony z poleceniem .

Informacje dotyczące kierowanego zdarzenia

Pole identyfikatora CanExecuteEvent
Strategia routingu Propagacji
Delegat CanExecuteRoutedEventHandler

Dotyczy

Produkt Wersje
.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

Zobacz też