--- title: "How to: Hook Up a Command to a Control with No Command Support" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "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" ms.assetid: dad08f64-700b-46fb-ad3f-fbfee95f0dfe --- # How to: Hook Up a Command to a Control with No Command Support The following example shows how to hook up a to a which does not have built in support for the command. For a complete sample which hooks up commands to multiple sources, see the [Create a Custom RoutedCommand Sample](https://github.com/Microsoft/WPF-Samples/tree/master/Input%20and%20Commands/CustomRoutedCommand) sample. ## Example [!INCLUDE[TLA#tla_winclient](../../../includes/tlasharptla-winclient-md.md)] provides a library of common commands which application programmers encounter regularly. The classes which comprise the command library are: , , , , and . The static objects which make up these classes do not supply command logic. The logic for the command is associated with the command with a . Many controls in [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] have built in support for some of the commands in the command library. , for example, supports many of the application edit commands such as , , , , and . The application developer does not have to do anything special to get these commands to work with these controls. If the is the command target when the command is executed, it will handle the command using the that is built into the control. The following shows how to use a as the command source for the command. A is created that associates the specified and the with the . First, the command source is created. A is used as the command source. [!code-xaml[commandWithHandler#CommandHandlerCommandSource](~/samples/snippets/csharp/VS_Snippets_Wpf/commandWithHandler/CSharp/Window1.xaml#commandhandlercommandsource)] [!code-csharp[CommandHandlerProcedural#CommandHandlerButtonCommandSource](~/samples/snippets/csharp/VS_Snippets_Wpf/CommandHandlerProcedural/CSharp/Window1.xaml.cs#commandhandlerbuttoncommandsource)] [!code-vb[CommandHandlerProcedural#CommandHandlerButtonCommandSource](~/samples/snippets/visualbasic/VS_Snippets_Wpf/CommandHandlerProcedural/visualbasic/window1.xaml.vb#commandhandlerbuttoncommandsource)] Next, the and the are created. The simply opens a to signify that the command executed. The sets the property to `true`. Normally, the can execute handler would perform more robust checks to see if the command could execute on the current command target. [!code-csharp[commandWithHandler#CommandHandlerBothHandlers](~/samples/snippets/csharp/VS_Snippets_Wpf/commandWithHandler/CSharp/Window1.xaml.cs#commandhandlerbothhandlers)] [!code-vb[commandWithHandler#CommandHandlerBothHandlers](~/samples/snippets/visualbasic/VS_Snippets_Wpf/commandWithHandler/VisualBasic/Window1.xaml.vb#commandhandlerbothhandlers)] Finally, a is created on the root of the application that associates the routed events handlers to the command. [!code-xaml[commandWithHandler#CommandHandlerCommandBinding](~/samples/snippets/csharp/VS_Snippets_Wpf/commandWithHandler/CSharp/Window1.xaml#commandhandlercommandbinding)] [!code-csharp[CommandHandlerProcedural#CommandHandlerBindingInit](~/samples/snippets/csharp/VS_Snippets_Wpf/CommandHandlerProcedural/CSharp/Window1.xaml.cs#commandhandlerbindinginit)] [!code-vb[CommandHandlerProcedural#CommandHandlerBindingInit](~/samples/snippets/visualbasic/VS_Snippets_Wpf/CommandHandlerProcedural/visualbasic/window1.xaml.vb#commandhandlerbindinginit)] ## See also - [Commanding Overview](commanding-overview.md) - [Hook Up a Command to a Control with Command Support](how-to-hook-up-a-command-to-a-control-with-command-support.md)