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 Custom Event CanExecute As CanExecuteRoutedEventHandler 
Public Event CanExecute As CanExecuteRoutedEventHandler 

範例

下列範例會建立將 ExecutedRoutedEventHandlerCanExecuteRoutedEventArgs 處理常式對應至 Open 命令的 CommandBindingThe 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. 如果命令目標沒有命令的 CommandBindingCanExecute 事件會向上反升專案樹狀目錄,以搜尋具有與命令相關聯之 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

適用於

另請參閱