The object that the command is being executed on.
Assembly: PresentationCore (in PresentationCore.dll)
In the Windows Presentation Foundation (WPF) commanding system, the CommandTarget property on a ICommandSource is only applicable when the ICommand is a RoutedCommand. If the CommandTarget is set on a ICommandSource and the corresponding command is not a RoutedCommand, the command target is ignored.
When used with a RoutedCommand, the command target is the object on which the Executed and CanExecute events are raised. If the property is not set, the element with keyboard focus will be used as the target.
This example shows how to create a command source by implementing ICommandSource. A command source is an object that knows how to invoke a command. The ICommandSource interface exposes three members: Command, CommandParameter, and . Command is the command which will be invoked. The CommandParameter is a user-defined data type which is passed from the command source to the method which handles the command. The is the object that the command is being executed on.
WPF provides a number of classes which implement ICommandSource, such as Button, MenuItem, and ListBoxItem. A command source defines how it invokes a command. Button and MenuItem invoke a command when they are clicked. A ListBoxItem invokes a command when it is double clicked. These classes only become a command source when their Command property is set.
For this example we will invoke the command when the slider is moved, or more accurately, when the Value property is changed.
The following is the class definition.
The next step is to implement the ICommandSource members. In this example, the properties are implemented as DependencyProperty objects. This enables the properties to use data binding. For more information about the DependencyProperty class, see the Dependency Properties Overview. For more information about data binding, see the Data Binding Overview.
Only the Command property is shown here.
The following is the DependencyProperty change callback.
The next step is to add and remove the command which is associated with the command source. The Command property cannot simply be overwritten when a new command is added, because the event handlers associated with the previous command, if there was one, must be removed first.
The CanExecuteChanged event notifies the command source that the ability of the command to execute on the current command target may have changed. When a command source receives this event, it typically calls the CanExecute method on the command. If the command cannot execute on the current command target, the command source will typically disable itself. If the command can execute on the current command target, the command source will typically enable itself.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.