Redigera

Dela via


ExecutedRoutedEventHandler Delegate

Definition

Represents the method that will handle the Executed and PreviewExecuted routed events, as well as related attached events.

public delegate void ExecutedRoutedEventHandler(System::Object ^ sender, ExecutedRoutedEventArgs ^ e);
public delegate void ExecutedRoutedEventHandler(object sender, ExecutedRoutedEventArgs e);
type ExecutedRoutedEventHandler = delegate of obj * ExecutedRoutedEventArgs -> unit
Public Delegate Sub ExecutedRoutedEventHandler(sender As Object, e As ExecutedRoutedEventArgs)

Parameters

sender
Object

The object where the event handler is attached.

e
ExecutedRoutedEventArgs

The event data.

Examples

This example creates an ExecutedRoutedEventHandler that sets a visual property on the target when the command is executed. The example also includes the CanExecuteRoutedEventHandler for the same command.

// ExecutedRoutedEventHandler for the custom color command.
private void ColorCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
    Panel target = e.Source as Panel;
    if (target != null)
    {
        if (target.Background == Brushes.AliceBlue)
        {
            target.Background = Brushes.LemonChiffon;
        }
        else
        {
            target.Background = Brushes.AliceBlue;
        }
    }
}

// CanExecuteRoutedEventHandler for the custom color command.
private void ColorCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    if (e.Source is Panel)
    {
        e.CanExecute = true;
    }
    else
    {
        e.CanExecute = false;
    }
}
' ExecutedRoutedEventHandler for the custom color command.
Private Sub ColorCmdExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    Dim target As Panel = TryCast(e.Source, Panel)
    If target IsNot Nothing Then
        If target.Background Is Brushes.AliceBlue Then
            target.Background = Brushes.LemonChiffon
        Else
            target.Background = Brushes.AliceBlue
        End If

    End If
End Sub

' CanExecuteRoutedEventHandler for the custom color command.
Private Sub ColorCmdCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
    If TypeOf e.Source Is Panel Then
        e.CanExecute = True
    Else
        e.CanExecute = False
    End If
End Sub

Remarks

This delegate contains the implementation logic for a RoutedCommand. Separating the implementation logic from the command allows the command to be invoked from different sources and types, and enables the centralization of command logic.

This delegate is also used for CommandManager.Executed and CommandManager.PreviewExecuted, which are attached events on the CommandManager class that implements much of the commanding infrastructure. But most practical handlers will handle the Executed events from a particular CommandBinding, rather than work at the CommandManager level.

Within ExecutedRoutedEventArgs, the following properties are generally of interest when you write an event handler for a routed event that results from an executed command:

  • Source reports the target where the command was executed. Once the command is executed, you can also think of the Source more generally in routed event terms, as the object that raised a routed event.

  • Command reports the command that executed. This property is useful if you use command bindings and if you write handlers that potentially handle multiple commands.

  • Parameter reports any command-specific parameters that were passed by the executing command. Not all commands use or expect command-specific parameters.

  • Handled reports whether the routed event that resulted from the executed command was already handled by a different element along the route. For routed event handlers, it is a recommended practice to have handlers that do meaningful work when handling the event to set Handled to true. This prevents typical handlers for the event from handling the event again further along the route. For more information on handling routed events, see Marking Routed Events as Handled, and Class Handling.

This delegate also represents handlers for CommandManager.Executed and CommandManager.PreviewExecutedEvent, which are attached events on the CommandManager class that implements much of the commanding infrastructure. But most practical handlers will handle the Executed events from a particular CommandBinding, rather than work at the CommandManager level.

For more information on commanding, see Commanding Overview.

Extension Methods

GetMethodInfo(Delegate)

Gets an object that represents the method represented by the specified delegate.

Applies to

See also