RoutedCommand.CanExecute(Object, IInputElement) Method

Definition

确定此 RoutedCommand 在其当前状态是否可以执行。Determines whether this RoutedCommand can execute in its current state.

public:
 bool CanExecute(System::Object ^ parameter, System::Windows::IInputElement ^ target);
public bool CanExecute (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

Parameters

parameter
Object

用户定义的数据类型。A user defined data type.

target
IInputElement

命令目标。The command target.

Returns

Boolean

如果可以对当前命令目标执行此命令,则为 true;否则为 falsetrue if the command can execute on the current command target; otherwise, false.

Attributes

Exceptions

Examples

下面的示例是 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,则会将其强制转换为 ICommandCanExecute 方法称为传递 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

Remarks

确定 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. 这些处理程序提供编程逻辑来确定是否可以执行 RoutedCommandThese handlers supply the programming logic for determining if the RoutedCommand can execute or not.

CommandTarget上引发 PreviewCanExecutePreviewExecuted 事件。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.

Applies to