Redigera

Dela via


RoutedCommand.CanExecute(Object, IInputElement) Method

Definition

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);
public bool CanExecute (object parameter, System.Windows.IInputElement target);
[<System.Security.SecurityCritical>]
member this.CanExecute : obj * System.Windows.IInputElement -> bool
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

true if the command can execute on the current command target; otherwise, false.

Attributes

Exceptions

Examples

The following example is a CanExecuteChanged event handler from a custom implementation of ICommandSource.

this.Command in this example is the Command property on the ICommandSource. If the command is not null, the command is cast to a RoutedCommand. If the command is a RoutedCommand, then the CanExecute method is called passing the CommandTarget and the CommandParameter. If command is not a RoutedCommand, it is cast to an ICommand and the CanExecute method is called passing the CommandParameter.

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

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. If a CommandBinding for that RoutedCommand is found, then the CanExecuteRoutedEventHandler attached to CommandBinding is called. These handlers supply the programming logic for determining if the RoutedCommand can execute or not.

The PreviewCanExecute and PreviewExecuted events are raised on the CommandTarget. If the CommandTarget is not set on the ICommandSource, the PreviewCanExecute and CanExecute events are raised on the element with keyboard focus.

Applies to