Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/how-to-hook-up-a-command-to-a-control-with-command-support.md
T
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.9 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Hook Up a Command to a Control with Command Support 03/30/2017
csharp
vb
Control class [WPF], attaching a RoutedCommand
classes [WPF], Control [WPF], attaching a RoutedCommand
RoutedCommand class [WPF], attaching to a Control
classes [WPF], RoutedCommand [WPF], attaching to a Control
8d8592ae-0c91-469e-a1cd-d179c4544548

How to: Hook Up a Command to a Control with Command Support

The following example shows how to hook up a xref:System.Windows.Input.RoutedCommand to a xref:System.Windows.Controls.Control which has built in support for the command. For a complete sample which hooks up commands to multiple sources, see the Create a Custom RoutedCommand Sample sample.

Example

[!INCLUDETLA#tla_winclient] provides a library of common commands which application programmers encounter regularly. The classes which comprise the command library are: xref:System.Windows.Input.ApplicationCommands, xref:System.Windows.Input.ComponentCommands, xref:System.Windows.Input.NavigationCommands, xref:System.Windows.Input.MediaCommands, and xref:System.Windows.Documents.EditingCommands.

The static xref:System.Windows.Input.RoutedCommand objects which make up these classes do not supply command logic. The logic for the command is associated with the command with a xref:System.Windows.Input.CommandBinding. Some controls have built in CommandBindings for some commands. This mechanism allows the semantics of a command to stay the same, while the actual implementation is can change. A xref:System.Windows.Controls.TextBox, for example, handles the xref:System.Windows.Input.ApplicationCommands.Paste%2A command differently than a control designed to support images, but the basic idea of what it means to paste something stays the same. The command logic cannot be supplied by the command, but rather must be supplied by the control or the application.

Many controls in [!INCLUDETLA2#tla_winclient] do have built in support for some of the commands in the command library. xref:System.Windows.Controls.TextBox, for example, supports many of the application edit commands such as xref:System.Windows.Input.ApplicationCommands.Paste%2A, xref:System.Windows.Input.ApplicationCommands.Copy%2A, xref:System.Windows.Input.ApplicationCommands.Cut%2A, xref:System.Windows.Input.ApplicationCommands.Redo%2A, and xref:System.Windows.Input.ApplicationCommands.Undo%2A. The application developer does not have to do anything special to get these commands to work with these controls. If the xref:System.Windows.Controls.TextBox is the command target when the command is executed, it will handle the command using the xref:System.Windows.Input.CommandBinding that is built into the control.

The following shows how to use a xref:System.Windows.Controls.MenuItem as the command source for the xref:System.Windows.Input.ApplicationCommands.Paste%2A command, where a xref:System.Windows.Controls.TextBox is the target of the command. All the logic that defines how the xref:System.Windows.Controls.TextBox performs the paste is built into the xref:System.Windows.Controls.TextBox control.

A xref:System.Windows.Controls.MenuItem is created and it's xref:System.Windows.Controls.MenuItem.Command%2A property is set to the xref:System.Windows.Input.ApplicationCommands.Paste%2A command. The xref:System.Windows.Controls.MenuItem.CommandTarget%2A is not explicitly set to the xref:System.Windows.Controls.TextBox object. When the xref:System.Windows.Controls.MenuItem.CommandTarget%2A is not set, the target for the command is the element which has keyboard focus. If the element which has keyboard focus does not support the xref:System.Windows.Input.ApplicationCommands.Paste%2A command or cannot currently execute the paste command (the clipboard is empty, for example) then the xref:System.Windows.Controls.MenuItem would be grayed out.

[!code-xamlMenuItemCommandTask_XAML#MenuItemCommanding]

[!code-csharpMenuItemCommandTask#MenuItemCommandingCodeBehind] [!code-vbMenuItemCommandTask#MenuItemCommandingCodeBehind]

See also