CommandManager.AddCanExecuteHandler Metoda

Definice

Připojí zadaný CanExecuteRoutedEventHandler prvek k zadanému elementu.

public:
 static void AddCanExecuteHandler(System::Windows::UIElement ^ element, System::Windows::Input::CanExecuteRoutedEventHandler ^ handler);
public static void AddCanExecuteHandler (System.Windows.UIElement element, System.Windows.Input.CanExecuteRoutedEventHandler handler);
static member AddCanExecuteHandler : System.Windows.UIElement * System.Windows.Input.CanExecuteRoutedEventHandler -> unit
Public Shared Sub AddCanExecuteHandler (element As UIElement, handler As CanExecuteRoutedEventHandler)

Parametry

element
UIElement

Prvek, ke který se má připojit handler .

handler
CanExecuteRoutedEventHandler

Může spustit obslužnou rutinu.

Výjimky

element nebo handler je null.

Příklady

Následující příklad vytvoří CanExecuteRoutedEventHandler a a ExecutedRoutedEventHandler a připojí je k objektu Button , který je zdrojem příkazů pro Help příkaz.

Nejprve se Button vytvoří a přidružuje k Help příkazu .

<Button Command="ApplicationCommands.Help"
        Name="helpButton">Help</Button>

Dále se CanExecuteRoutedEventHandler vytvoří objekty ExecutedRoutedEventHandler a .

private void HelpCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
    // OpenHelpFile opens the help file
    OpenHelpFile();
}
Private Sub HelpCmdExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    ' OpenHelpFile opens the help file
    OpenHelpFile()
End Sub
private void HelpCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    // HelpFilesExists() determines if the help file exists
    if (HelpFileExists() == true)
    {
        e.CanExecute = true;
    }
    else
    {
        e.CanExecute = false;
    }
}
Private Sub HelpCmdCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
    ' HelpFilesExists() determines if the help file exists
    If HelpFileExists() = True Then
        e.CanExecute = True
    Else
        e.CanExecute = False
    End If
End Sub

A nakonec jsou obslužné rutiny připojeny k nástroji ButtonAddCanExecuteHandler pomocí a AddExecutedHandler.

CommandManager.AddExecutedHandler(helpButton, HelpCmdExecuted);
CommandManager.AddCanExecuteHandler(helpButton, HelpCmdCanExecute);
CommandManager.AddExecutedHandler(helpButton, AddressOf HelpCmdExecuted)
CommandManager.AddCanExecuteHandler(helpButton, AddressOf HelpCmdCanExecute)

Platí pro

Viz také