Files
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

4.7 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Create a RoutedCommand 03/30/2017
csharp
vb
RoutedCommand class [WPF], creating
aaf6979f-69ab-406f-979f-5766daa85fa0

How to: Create a RoutedCommand

This example shows how to create a custom xref:System.Windows.Input.RoutedCommand and how to implement the custom command by creating a xref:System.Windows.Input.ExecutedRoutedEventHandler and a xref:System.Windows.Input.CanExecuteRoutedEventHandler and attaching them to a xref:System.Windows.Input.CommandBinding. For more information on commanding, see the Commanding Overview.

Example

The first step in creating a xref:System.Windows.Input.RoutedCommand is defining the command and instantiating it.

[!code-csharpCommandingOverviewSnippets#CommandingOverviewCommandDefinition] [!code-vbCommandingOverviewSnippets#CommandingOverviewCommandDefinition]

In order to use the command in an application, event handlers which define what the command does must be created

[!code-csharpCommandingOverviewSnippets#CommandingOverviewExecuted] [!code-vbCommandingOverviewSnippets#CommandingOverviewExecuted]

[!code-csharpCommandingOverviewSnippets#CommandingOverviewCanExecute] [!code-vbCommandingOverviewSnippets#CommandingOverviewCanExecute]

Next, a xref:System.Windows.Input.CommandBinding is created which associates the command with the event handlers. The xref:System.Windows.Input.CommandBinding is created on a specific object. This object defines the scope of the xref:System.Windows.Input.CommandBinding in the element tree

[!code-xamlCommandingOverviewSnippets#CommandingOverviewWindowCommandBindingXAML]

[!code-csharpCommandingOverviewSnippets#CommandingOverviewCustomCommandBindingCodeBehind] [!code-vbCommandingOverviewSnippets#CommandingOverviewCustomCommandBindingCodeBehind]

The final step is invoking the command. One way to invoke a command is to associate it with a xref:System.Windows.Input.ICommandSource, such as a xref:System.Windows.Controls.Button.

[!code-xamlCommandingOverviewSnippets#CommandingOverviewCustomCommandSourceXAML]

[!code-csharpCommandingOverviewSnippets#CommandingOverviewCustomCommandSourceCodeBehind] [!code-vbCommandingOverviewSnippets#CommandingOverviewCustomCommandSourceCodeBehind]

When the Button is clicked, the xref:System.Windows.Input.RoutedCommand.Execute%2A method on the custom xref:System.Windows.Input.RoutedCommand is called. The xref:System.Windows.Input.RoutedCommand raises the xref:System.Windows.Input.CommandManager.PreviewExecuted and xref:System.Windows.Input.CommandManager.Executed routed events. These events traverse the element tree looking for a xref:System.Windows.Input.CommandBinding for this particular command. If a xref:System.Windows.Input.CommandBinding is found, the xref:System.Windows.Input.ExecutedRoutedEventHandler associated with xref:System.Windows.Input.CommandBinding is called.

See also