如何:启用命令

以下示例演示如何在 Windows Presentation Foundation(WPF)中使用命令。 该示例演示如何将RoutedCommand关联到Button,创建CommandBinding,以及创建实现RoutedCommand的事件处理程序。 有关命令的详细信息,请参阅 命令概述

示例:

代码的第一部分创建用户界面(UI),该用户界面由ButtonStackPanel组成,并创建一个CommandBinding,将命令处理程序与RoutedCommand关联。

Command 属性 ButtonClose 命令相关联。

CommandBinding 添加到根 CommandBindingCollectionWindowExecutedCanExecute事件处理程序附加到此绑定,并与Close命令相关联。

CommandBinding如果没有命令逻辑,则只有调用命令的机制。 Button点击时,将在命令目标上触发PreviewExecutedRoutedEvent,随后执行命令ExecutedRoutedEvent。 这些事件在元素树中遍历查找特定命令的 CommandBinding。 值得注意的是,由于 RoutedEvent 隧道和气泡穿过元素树,因此必须小心选择 CommandBinding 的放置位置。 CommandBinding如果位于命令目标的同级节点或不在路由RoutedEvent上的另一个节点上,CommandBinding则不会访问该节点。

<Window x:Class="WCSamples.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://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.
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);
' 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)

代码的下一部分实现 ExecutedCanExecute 事件处理程序。

处理程序 Executed 调用方法以关闭打开的文件。 处理程序 CanExecute 调用一个方法来确定是否打开文件。 如果文件处于打开状态, CanExecute 则设置为 true;否则,它设置为 false

// 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;
    }
}
' 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

另请参阅