Partager via


CommandManager.AddExecutedHandler Méthode

Définition

Attache le ExecutedRoutedEventHandler spécifié à l'élément spécifié.

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

Paramètres

element
UIElement

Élément auquel attacher handler.

handler
ExecutedRoutedEventHandler

Gestionnaire exécuté.

Exceptions

element ou handler est null.

Exemples

L’exemple suivant crée un CanExecuteRoutedEventHandler et un ExecutedRoutedEventHandler et les attache à un Button qui est une source de commande pour la Help commande.

Tout d’abord, le Button est créé et associé à la Help commande.

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

CanExecuteRoutedEventHandler et ExecutedRoutedEventHandler sont ensuite créés.

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

Enfin, les gestionnaires sont attachés à l’à l’aide de ButtonAddCanExecuteHandler et .AddExecutedHandler

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

S’applique à

Voir aussi