Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/how-to-implement-icommandsource.md
T
Andy (Steve) De George e9fcf43083 Merge pull request #1135 from dotnet/adegeo-links
Correct .NET Framework links that pointed to old .NET 5 URLs
2021-08-11 12:04:34 -07:00

6.3 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Implement ICommandSource 12/05/2019
csharp
vb
ICommandSource interfaces [WPF], implementing
7452dd39-6e11-44bf-806a-31d87f3772ac

How to: Implement ICommandSource

This example shows how to create a command source by implementing xref:System.Windows.Input.ICommandSource. A command source is an object that knows how to invoke a command. The xref:System.Windows.Input.ICommandSource interface exposes three members:

In this example, a class is created that inherits from the xref:System.Windows.Controls.Slider control and implements the xref:System.Windows.Input.ICommandSource interface.

Example

WPF provides a number of classes which implement xref:System.Windows.Input.ICommandSource, such as xref:System.Windows.Controls.Button, xref:System.Windows.Controls.MenuItem, and xref:System.Windows.Documents.Hyperlink. A command source defines how it invokes a command. These classes invoke a command when they're clicked and they only become a command source when their xref:System.Windows.Input.ICommandSource.Command%2A property is set.

In this example, you'll invoke the command when the slider is moved, or more accurately, when the xref:System.Windows.Controls.Primitives.RangeBase.Value%2A property is changed.

The following is the class definition:

[!code-csharpImplementICommandSource#ImplementICommandSourceClassDefinition] [!code-vbImplementICommandSource#ImplementICommandSourceClassDefinition]

The next step is to implement the xref:System.Windows.Input.ICommandSource members. In this example, the properties are implemented as xref:System.Windows.DependencyProperty objects. This enables the properties to use data binding. For more information about the xref:System.Windows.DependencyProperty class, see the Dependency Properties Overview. For more information about data binding, see the Data Binding Overview.

Only the xref:System.Windows.Input.ICommandSource.Command%2A property is shown here.

[!code-csharpImplementICommandSource#ImplementICommandSourceCommandPropertyDefinition] [!code-vbImplementICommandSource#ImplementICommandSourceCommandPropertyDefinition]

The following is the xref:System.Windows.DependencyProperty change callback:

[!code-csharpImplementICommandSource#ImplementICommandSourceCommandChanged] [!code-vbImplementICommandSource#ImplementICommandSourceCommandChanged]

The next step is to add and remove the command which is associated with the command source. The xref:System.Windows.Input.ICommandSource.Command%2A 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.

[!code-csharpImplementICommandSource#ImplementICommandSourceHookUnHookCommands] [!code-vbImplementICommandSource#ImplementICommandSourceHookUnHookCommands]

The next step is to create logic for the xref:System.Windows.Input.ICommand.CanExecuteChanged handler.

The xref:System.Windows.Input.ICommand.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 xref:System.Windows.Input.ICommand.CanExecute%2A 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.

[!code-csharpImplementICommandSource#ImplementICommandCanExecuteChanged] [!code-vbImplementICommandSource#ImplementICommandCanExecuteChanged]

The last step is the xref:System.Windows.Input.ICommand.Execute%2A method. If the command is a xref:System.Windows.Input.RoutedCommand, the xref:System.Windows.Input.RoutedCommand xref:System.Windows.Input.RoutedCommand.Execute%2A method is called; otherwise, the xref:System.Windows.Input.ICommand xref:System.Windows.Input.ICommand.Execute%2A method is called.

[!code-csharpImplementICommandSource#ImplementICommandExecute] [!code-vbImplementICommandSource#ImplementICommandExecute]

See also