Dela via


How to: Enable a Command

The following example demonstrates how to use commanding in Windows Presentation Foundation (WPF). The example shows how to associate a RoutedCommand to a Button, create a CommandBinding, and create the event handlers which implement the RoutedCommand. For more information on commanding, see the Commanding Overview.

Example

The first section of code creates the user interface (UI), which consists of a Button and a StackPanel, and creates a CommandBinding that associates the command handlers with the RoutedCommand.

The Command property of the Button is associated with the Close command.

The CommandBinding is added to the CommandBindingCollection of the root Window. The Executed and CanExecute event handlers are attached to this binding and associated with the Close command.

Without the CommandBinding there is no command logic, only a mechanism to invoke the command. When the Button is clicked, the PreviewExecuted RoutedEvent is raised on the command target followed by the Executed RoutedEvent. These events traverse the element tree looking for a CommandBinding for that particular command. It is worth noting that because RoutedEvent tunnel and bubble through the element tree, care must be taken in where the CommandBinding is put. If the CommandBinding is on a sibling of the command target or another node that is not on the route of the RoutedEvent, the CommandBinding will not be accessed.

<Window x:Class="WCSamples.Window1"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    Title="CloseCommand"
    Name="RootWindow"
    >
  <Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Close"
                    Executed="CloseCommandHandler"
                    CanExecute="CanExecuteHandler"
                    />
  </Window.CommandBindings>
  <StackPanel Name="MainStackPanel">
    <Button Command="ApplicationCommands.Close" 
            Content="Close File" />
  </StackPanel>
</Window>
            ' Create ui elements.
            Dim CloseCmdStackPanel As New StackPanel()
            Dim CloseCmdButton As New Button()
            CloseCmdStackPanel.Children.Add(CloseCmdButton)

            ' Set Button's properties.
            CloseCmdButton.Content = "Close File"
            CloseCmdButton.Command = ApplicationCommands.Close

            ' Create the CommandBinding.
            Dim CloseCommandBinding As New CommandBinding(ApplicationCommands.Close, AddressOf CloseCommandHandler, AddressOf CanExecuteHandler)

            ' Add the CommandBinding to the root Window.
            RootWindow.CommandBindings.Add(CloseCommandBinding)
// Create ui elements.
StackPanel CloseCmdStackPanel = new StackPanel();
Button CloseCmdButton = new Button();
CloseCmdStackPanel.Children.Add(CloseCmdButton);

// Set Button's properties.
CloseCmdButton.Content = "Close File";
CloseCmdButton.Command = ApplicationCommands.Close;

// Create the CommandBinding.
CommandBinding CloseCommandBinding = new CommandBinding(
    ApplicationCommands.Close, CloseCommandHandler, CanExecuteHandler);

// Add the CommandBinding to the root Window.
RootWindow.CommandBindings.Add(CloseCommandBinding);

The next section of code implements the Executed and CanExecute event handlers.

The Executed handler calls a method to close the open file. The CanExecute handler calls a method to determine whether a file is open. If a file is open, CanExecute is set to true; otherwise, it is set to false.

        ' Executed event handler.
        Private Sub CloseCommandHandler(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
            ' Calls a method to close the file and release resources.
            CloseFile()
        End Sub

        ' CanExecute event handler.
        Private Sub CanExecuteHandler(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
            ' Call a method to determine if there is a file open.
            ' If there is a file open, then set CanExecute to true.
            If IsFileOpened() Then
                e.CanExecute = True
            ' if there is not a file open, then set CanExecute to false.
            Else
                e.CanExecute = False
            End If
        End Sub
// Executed event handler.
private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
    // Calls a method to close the file and release resources.
    CloseFile();
}

// CanExecute event handler.
private void CanExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
{
    // Call a method to determine if there is a file open.
    // If there is a file open, then set CanExecute to true.
    if (IsFileOpened())
    {
        e.CanExecute = true;
    }
    // if there is not a file open, then set CanExecute to false.
    else
    {
        e.CanExecute = false;
    }
}

See Also

Concepts

Commanding Overview