CommandBinding.Executed 事件

定义

执行与此 CommandBinding 相关联的命令时发生。Occurs when the command associated with this CommandBinding executes.

public:
 event System::Windows::Input::ExecutedRoutedEventHandler ^ Executed;
public event System.Windows.Input.ExecutedRoutedEventHandler Executed;
member this.Executed : System.Windows.Input.ExecutedRoutedEventHandler 
Public Event Executed As ExecutedRoutedEventHandler 

示例

下面的示例创建一个CommandBinding ,它将ExecutedRoutedEventHandler OpenCanExecuteRoutedEventArgs处理程序映射到命令。The following example creates a CommandBinding that maps an ExecutedRoutedEventHandler and a CanExecuteRoutedEventArgs handler to the Open command.

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

下面演示了在ExecutedRoutedEventHandler执行命令时MessageBox创建的。The following shows the ExecutedRoutedEventHandler which creates a MessageBox when the command is executed.

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

注解

当执行时,它会在PreviewExecuted命令目标上引发事件。 RoutedCommandWhen a RoutedCommand executes, it raises the PreviewExecuted event on the command target. 如果未PreviewExecuted处理该事件Executed ,则会在命令目标上引发事件。If the PreviewExecuted event is not handled, the Executed event is raised on the command target. 如果命令目标CommandBinding对于特定的命令具有Executed ,则调用的CommandBinding处理程序。If the command target has a CommandBinding for the specific command, the Executed handler for that CommandBinding is called. 如果命令目标CommandBinding对于特定的命令没有,则该Executed事件将向上冒泡元素树,搜索包含CommandBinding与命令相关联的元素。If the command target does not have a CommandBinding for that specific command the Executed event bubbles up the element tree searching for an element that has a CommandBinding associated with the command. CommandBinding如果未找到,则不处理该命令。If a CommandBinding is not found, the command is not handled.

路由事件信息Routed Event Information

标识符字段Identifier field ExecutedEvent
路由策略Routing strategy 冒泡Bubbling
委托Delegate ExecutedRoutedEventHandler

适用于

另请参阅