共用方式為


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建立 ,將 和 CanExecuteRoutedEventArgs 處理程式對應ExecutedRoutedEventHandlerOpen 命令。

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

以下顯示設定為 trueCanExecuteRoutedEventHandlerCanExecute

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

適用於

另請參閱