CommandBinding.CanExecute 事件

定义

在与此 CommandBinding 关联的命令开始检查能否对命令目标执行该命令时发生。Occurs when the command associated with this CommandBinding initiates a check to determine whether the command can be executed on the command target.

public:
 event System::Windows::Input::CanExecuteRoutedEventHandler ^ CanExecute;
public event System.Windows.Input.CanExecuteRoutedEventHandler CanExecute;
member this.CanExecute : System.Windows.Input.CanExecuteRoutedEventHandler 
Public Event CanExecute As CanExecuteRoutedEventHandler 

示例

下面的示例创建一个 CommandBinding,它将 ExecutedRoutedEventHandlerCanExecuteRoutedEventArgs 处理程序映射到 Open 命令。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)

下面显示了将 CanExecute 设置为 trueCanExecuteRoutedEventHandlerThe following shows the CanExecuteRoutedEventHandler which sets CanExecute to true.

void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}
Private Sub OpenCmdCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
    e.CanExecute = True
End Sub

注解

调用 RoutedCommand 上的 CanExecute 方法时,会在命令目标上引发 PreviewCanExecute 事件。When the CanExecute method on a RoutedCommand is called, the PreviewCanExecute event is raised on the command target. 如果未处理该事件,则会引发 CanExecute 事件。If the event is not handled, the CanExecute event is raised. 如果命令目标具有该命令的 CommandBinding,则将调用该 CommandBindingCanExecute 处理程序。If the command target has a CommandBinding for the command, the CanExecute handler for that CommandBinding is called. 如果命令目标没有命令的 CommandBinding,则 CanExecute 事件会向上冒泡元素树,搜索包含与该命令关联的 CommandBinding 的元素。If the command target does not have a CommandBinding for the command, the CanExecute event bubbles up the element tree searching for an element that has a CommandBinding associated with the command.

路由事件信息Routed Event Information

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

适用于

另请参阅