共用方式為


HOW TO:啟用命令

下列範例示範如何在 Windows Presentation Foundation (WPF) 中使用命令。 範例顯示如何建立 RoutedCommandButton 的關聯、建立 CommandBinding,以及建立實作 RoutedCommand 的事件處理常式。 如需使用命令的詳細資訊,請參閱命令概觀

範例

程式碼的第一個區段會建立由 ButtonStackPanel 組成的user interface (UI),並建立會將命令處理常式與 RoutedCommand 相關聯的 CommandBinding

ButtonCommand 屬性與 Close 命令相關聯。

CommandBinding 是加入到根 WindowCommandBindingCollectionExecutedCanExecute 事件處理常式會附加到這個繫結,並與 Close 命令相關聯。

沒有 CommandBinding,就沒有命令邏輯,只有用於叫用命令的機制。 當按一下 Button 時,會在命令目標上隨著 Executed RoutedEvent 後引發 PreviewExecuted RoutedEvent。 這些事件會在項目樹狀結構中周遊,以查看該特定命令的 CommandBinding。 值得注意的是,因為 RoutedEvent 會在項目樹狀結構中進行通道和反昇,必須在放置 CommandBinding 的地方特別留意。 如果 CommandBinding 位於命令目標的同層級 (Sibling) 中,或是位於不在 RoutedEvent 的路由的其他節點中,就不會存取 CommandBinding

<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);

下一個程式碼區段會實作 ExecutedCanExecute 事件處理常式。

Executed 處理常式會呼叫方法關閉開啟的檔案。 CanExecute 處理常式會呼叫方法判斷檔案是否開啟。 如果檔案是開啟的,CanExecute 會設定為 true,否則就設定為 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;
    }
}

請參閱

概念

命令概觀