* 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
4.1 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||
|---|---|---|---|---|---|---|---|---|
| How to: Enable a Command | 03/30/2017 |
|
|
d8016266-58d9-48f7-8298-a86b7ed49fbd |
How to: Enable a Command
The following example demonstrates how to use commanding in [!INCLUDETLA#tla_winclient]. The example shows how to associate a xref:System.Windows.Input.RoutedCommand to a xref:System.Windows.Controls.Button, create a xref:System.Windows.Input.CommandBinding, and create the event handlers which implement the xref:System.Windows.Input.RoutedCommand. For more information on commanding, see the Commanding Overview.
Example
The first section of code creates the [!INCLUDETLA#tla_ui], which consists of a xref:System.Windows.Controls.Button and a xref:System.Windows.Controls.StackPanel, and creates a xref:System.Windows.Input.CommandBinding that associates the command handlers with the xref:System.Windows.Input.RoutedCommand.
The xref:System.Windows.Input.ICommandSource.Command%2A property of the xref:System.Windows.Controls.Button is associated with the xref:System.Windows.Input.ApplicationCommands.Close%2A command.
The xref:System.Windows.Input.CommandBinding is added to the xref:System.Windows.Input.CommandBindingCollection of the root xref:System.Windows.Window. The xref:System.Windows.Input.CommandBinding.Executed and xref:System.Windows.Input.CommandBinding.CanExecute event handlers are attached to this binding and associated with the xref:System.Windows.Input.ApplicationCommands.Close%2A command.
Without the xref:System.Windows.Input.CommandBinding there is no command logic, only a mechanism to invoke the command. When the xref:System.Windows.Controls.Button is clicked, the xref:System.Windows.Input.CommandManager.PreviewExecuted xref:System.Windows.RoutedEvent is raised on the command target followed by the xref:System.Windows.Input.CommandManager.Executed xref:System.Windows.RoutedEvent. These events traverse the element tree looking for a xref:System.Windows.Input.CommandBinding for that particular command. It is worth noting that because xref:System.Windows.RoutedEvent tunnel and bubble through the element tree, care must be taken in where the xref:System.Windows.Input.CommandBinding is put. If the xref:System.Windows.Input.CommandBinding is on a sibling of the command target or another node that is not on the route of the xref:System.Windows.RoutedEvent, the xref:System.Windows.Input.CommandBinding will not be accessed.
[!code-xamlEnableCloseCommand#CloseCommandBinding]
[!code-csharpEnableCloseCommand#CloseCommandBindingCodeBehind] [!code-vbEnableCloseCommand#CloseCommandBindingCodeBehind]
The next section of code implements the xref:System.Windows.Input.CommandManager.Executed and xref:System.Windows.Input.CommandBinding.CanExecute event handlers.
The xref:System.Windows.Input.CommandManager.Executed handler calls a method to close the open file. The xref:System.Windows.Input.CommandBinding.CanExecute handler calls a method to determine whether a file is open. If a file is open, xref:System.Windows.Input.CanExecuteRoutedEventArgs.CanExecute%2A is set to true; otherwise, it is set to false.
[!code-csharpEnableCloseCommand#CloseCommandHandler] [!code-vbEnableCloseCommand#CloseCommandHandler]