RoutedCommand.CanExecute(Object, IInputElement) 方法

定義

判斷這個 RoutedCommand 是否能在其目前狀態中執行。Determines whether this RoutedCommand can execute in its current state.

public:
 bool CanExecute(System::Object ^ parameter, System::Windows::IInputElement ^ target);
[System.Security.SecurityCritical]
public bool CanExecute (object parameter, System.Windows.IInputElement target);
member this.CanExecute : obj * System.Windows.IInputElement -> bool
Public Function CanExecute (parameter As Object, target As IInputElement) As Boolean

參數

parameter
Object

使用者定義資料型別。A user defined data type.

target
IInputElement

命令目標。The command target.

傳回

如果命令可以在目前命令目標上執行,則為 true,否則為 falsetrue if the command can execute on the current command target; otherwise, false.

屬性

例外狀況

target 不是 UIElement 也不是 ContentElementtarget is not a UIElement or ContentElement.

範例

下列範例是來自 ICommandSource自訂執行的 CanExecuteChanged 事件處理常式。The following example is a CanExecuteChanged event handler from a custom implementation of ICommandSource.

此範例中的 this.CommandICommandSource上的 Command 屬性。this.Command in this example is the Command property on the ICommandSource. 如果未 null命令,則會將命令轉換成 RoutedCommandIf the command is not null, the command is cast to a RoutedCommand. 如果命令是 RoutedCommand,則會呼叫 CanExecute 方法來傳遞 CommandTargetCommandParameterIf the command is a RoutedCommand, then the CanExecute method is called passing the CommandTarget and the CommandParameter. 如果命令不是 RoutedCommand,它會轉換成 ICommand,而 CanExecute 方法則稱為傳遞 CommandParameterIf command is not a RoutedCommand, it is cast to an ICommand and the CanExecute method is called passing the CommandParameter.

如果 CanExecute 方法傳回 true,則控制項會啟用;否則,控制項會停用。If the CanExecute method returns true, then the control is enabled; otherwise, the control is disable.

private void CanExecuteChanged(object sender, EventArgs e)
{

    if (this.Command != null)
    {
        RoutedCommand command = this.Command as RoutedCommand;

        // If a RoutedCommand.
        if (command != null)
        {
            if (command.CanExecute(CommandParameter, CommandTarget))
            {
                this.IsEnabled = true;
            }
            else
            {
                this.IsEnabled = false;
            }
        }
        // If a not RoutedCommand.
        else
        {
            if (Command.CanExecute(CommandParameter))
            {
                this.IsEnabled = true;
            }
            else
            {
                this.IsEnabled = false;
            }
        }
    }
}
Private Sub CanExecuteChanged(ByVal sender As Object, ByVal e As EventArgs)

    If Me.Command IsNot Nothing Then
        Dim command As RoutedCommand = TryCast(Me.Command, RoutedCommand)

        ' If a RoutedCommand.
        If command IsNot Nothing Then
            If command.CanExecute(CommandParameter, CommandTarget) Then
                Me.IsEnabled = True
            Else
                Me.IsEnabled = False
            End If
            ' If a not RoutedCommand.
        Else
            If Me.Command.CanExecute(CommandParameter) Then
                Me.IsEnabled = True
            Else
                Me.IsEnabled = False
            End If
        End If
    End If
End Sub

備註

判斷 RoutedCommand 是否可在目前命令目標上執行的實際邏輯並未包含在 CanExecute 方法中,而是 CanExecute 引發 PreviewCanExecuteCanExecute 事件,這會透過專案樹狀結構來尋找具有 CommandBinding之物件的通道和冒泡。The actual logic that determines if a RoutedCommand can execute on the current command target is not contained in the CanExecute methods, rather CanExecute raises the PreviewCanExecute and the CanExecute events which tunnel and bubble through element tree looking for a object with a CommandBinding. 如果找到該 RoutedCommandCommandBinding,則會呼叫附加至 CommandBindingCanExecuteRoutedEventHandlerIf a CommandBinding for that RoutedCommand is found, then the CanExecuteRoutedEventHandler attached to CommandBinding is called. 這些處理常式會提供程式設計邏輯,以判斷 RoutedCommand 是否可以執行。These handlers supply the programming logic for determining if the RoutedCommand can execute or not.

PreviewCanExecutePreviewExecuted 事件會在 CommandTarget上引發。The PreviewCanExecute and PreviewExecuted events are raised on the CommandTarget. 如果未在 ICommandSource上設定 CommandTarget,則 PreviewCanExecuteCanExecute 事件會在具有鍵盤焦點的元素上引發。If the CommandTarget is not set on the ICommandSource, the PreviewCanExecute and CanExecute events are raised on the element with keyboard focus.

適用於