Cette page vous a-t-elle été utile ?
Votre avis sur ce contenu est important. N'hésitez pas à nous faire part de vos commentaires.
Vous avez d'autres commentaires ?
1500 caractères restants
Exporter (0) Imprimer
Développer tout

RoutedCommand.Execute, méthode

Executes the RoutedCommand on the current command target.

Espace de noms: System.Windows.Input
Assembly : PresentationCore (dans presentationcore.dll)

public void Execute (
	Object parameter,
	IInputElement target
)
public void Execute (
	Object parameter, 
	IInputElement target
)
public function Execute (
	parameter : Object, 
	target : IInputElement
)
Vous ne pouvez pas utiliser des méthodes dans XAML.

Paramètres

parameter

User defined parameter to be passed to the handler.

target

Element at which to being looking for command handlers.

Type d'exceptionCondition

InvalidOperationException

target is not a UIElement or ContentElement.

The actual logic that executes the RoutedCommand is not contained in the Execute methods. Execute raises the PreviewExecuted and Executed events which tunnel and bubble through element tree looking for a object with a CommandBinding. If a CommandBinding for that RoutedCommand is found, then the ExecutedRoutedEventHandler attached to CommandBinding is called. These handlers supply the programming logic that performs the RoutedCommand.

The PreviewExecuted and Executed events are raised on the CommandTarget. If the CommandTarget is not set on the ICommandSource, the PreviewExecuted and Executed events are raised on the element with keyboard focus.

The following example is from a custom implementation of ICommandSource sample. The full sample subclasses the Slider class and implements ICommandSource to create a slider that invokes a command when its value property is changed. For the complete sample, see Implement ICommand Source Sample.

This.Command in this example is the Command property on the ICommandSource. If the command is not null, the command is cast to a RoutedCommand. If it is a RoutedCommand, then the Execute method is called passing the CommandTarget and the CommandParameter. If the command is not a RoutedCommand, it is cast to an ICommand and the Execute method is called passing the CommandParameter.

// If Command is defined, moving the slider will invoke the command;
// Otherwise, the slider will behave normally.
protected override void OnValueChanged(double oldValue, double newValue)
{
    base.OnValueChanged(oldValue, newValue);

    if (this.Command != null)
    {
        RoutedCommand command = Command as RoutedCommand;

        if (command != null)
        {
            command.Execute(CommandParameter, CommandTarget);
        }
        else
        {
            ((ICommand)Command).Execute(CommandParameter);
        }
    }
}

Microsoft .NET Framework 3.0 est pris en charge sur Windows Vista, Microsoft Windows XP SP2 et Windows Server 2003 SP1.

.NET Framework

Prise en charge dans : 3.0

Ajouts de la communauté

AJOUTER
Afficher:
© 2015 Microsoft