CommandBinding.CanExecute 事件

定义

在与此 CommandBinding 关联的命令开始检查能否对命令目标执行该命令时发生。

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

事件类型

示例

以下示例创建一个 , CommandBinding 用于将 ExecutedRoutedEventHandlerCanExecuteRoutedEventArgs 处理程序映射到 命令 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)

下面显示了将 CanExecuteRoutedEventHandler 设置为 CanExecutetrue的 。

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

注解

CanExecute调用 上的 RoutedCommand 方法时,将在PreviewCanExecute命令目标上引发 事件。 如果未处理事件,则会引发 该 CanExecute 事件。 如果命令目标具有 CommandBinding 命令的 ,则 CanExecute 调用 的 CommandBinding 处理程序。 如果命令目标没有 CommandBinding 命令的 ,则事件将 CanExecute 向上弹出元素树,搜索具有 CommandBinding 与命令关联的 的 元素。

路由事件信息

标识符字段 CanExecuteEvent
路由策略 鼓 泡
委托 CanExecuteRoutedEventHandler

适用于

另请参阅