Share via


如何:將命令與沒有命令支援的控制項連結

下列範例示範如何將 RoutedCommand 與沒有命令內建支援的 Control 連結。 如需將命令連結至多個來源的完整範例,請參閱建立自訂的 RoutedCommand 範例範例。

範例

Windows Presentation Foundation (WPF) 提供常見的命令程式庫,應用程式程式設計人員會定期遇到這些命令。 命令程式庫包含的類別如下:ApplicationCommandsComponentCommandsNavigationCommandsMediaCommandsEditingCommands

構成這些類別的靜態 RoutedCommand 物件不提供命令邏輯。 此命令的邏輯會與具有 CommandBinding 的命令建立關聯。 WPF 中的許多控制項都內建了命令程式庫中某些命令的支援。 比方說,TextBox 支援許多應用程式編輯命令,例如PasteCopyCutRedoUndo。 應用程式開發人員不需要特別執行任何作業,即可取得這些命令來使用這些控制項。 如果 TextBox 在執行命令時是命令目標,它將使用控制項內建的 CommandBinding 來處理命令。

下列範例示範如何使用 Button 作為 Open 命令的命令來源。 會建立一個 CommandBinding,將指定的 CanExecuteRoutedEventHandlerCanExecuteRoutedEventHandlerRoutedCommand 建立關聯。

首先,會建立命令來源。 將使用 Button 作為命令來源。

<Button Command="ApplicationCommands.Open" Name="MyButton"
        Height="50" Width="200">
  Open (KeyBindings: Ctrl+R, Ctrl+0)
</Button>
// Button used to invoke the command
Button CommandButton = new Button();
CommandButton.Command = ApplicationCommands.Open;
CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)";
MainStackPanel.Children.Add(CommandButton);
' Button used to invoke the command
Dim CommandButton As New Button()
CommandButton.Command = ApplicationCommands.Open
CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)"
MainStackPanel.Children.Add(CommandButton)

接下來,會建立 ExecutedRoutedEventHandlerCanExecuteRoutedEventHandlerExecutedRoutedEventHandler 只會開啟 MessageBox 來表示已執行命令。 CanExecuteRoutedEventHandler 會將 CanExecute 屬性設定為 true。 一般來說,可以執行處理常式會執行更穩固的檢查,以查看命令是否無法在目前命令目標上執行。


void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
{
    String command, targetobj;
    command = ((RoutedCommand)e.Command).Name;
    targetobj = ((FrameworkElement)target).Name;
    MessageBox.Show("The " + command +  " command has been invoked on target object " + targetobj);
}
void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}


Private Sub OpenCmdExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    Dim command, targetobj As String
    command = CType(e.Command, RoutedCommand).Name
    targetobj = CType(sender, FrameworkElement).Name
    MessageBox.Show("The " + command + " command has been invoked on target object " + targetobj)
End Sub
Private Sub OpenCmdCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
    e.CanExecute = True
End Sub

最後,會在應用程式的根 Window 上建立 CommandBinding,將路由的事件處理常式與 Open 命令建立關聯。

<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>
// Creating CommandBinding and attaching an Executed and CanExecute handler
CommandBinding OpenCmdBinding = new CommandBinding(
    ApplicationCommands.Open,
    OpenCmdExecuted,
    OpenCmdCanExecute);

this.CommandBindings.Add(OpenCmdBinding);
' Creating CommandBinding and attaching an Executed and CanExecute handler
Dim OpenCmdBinding As New CommandBinding(ApplicationCommands.Open, AddressOf OpenCmdExecuted, AddressOf OpenCmdCanExecute)

Me.CommandBindings.Add(OpenCmdBinding)

另請參閱