RoutedCommand.CanExecute(Object, IInputElement) 메서드

정의

RoutedCommand가 현재 상태에서 실행될 수 있는지 여부를 확인합니다.

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

매개 변수

parameter
Object

사용자 정의 데이터 형식입니다.

target
IInputElement

명령 대상입니다.

반환

현재 명령 대상에서 명령을 실행할 수 있으면 true이고, 그렇지 않으면 false입니다.

특성

예외

targetUIElement 또는 ContentElement가 아닌 경우

예제

다음 예제는 의 사용자 지정 구현에서 이벤트 처리기입니다 CanExecuteChangedICommandSource.

this.Command 이 예제에서는 의 Command 속성입니다 ICommandSource. 명령이 이 아니면 null명령이 로 RoutedCommand캐스팅됩니다. 명령이 이 RoutedCommand면 메서드가 CanExecute 및 을 CommandTarget 전달합니다 CommandParameter. 명령이 가 아닌 경우 으로 RoutedCommandICommand 캐스팅되고 메서드가 CanExecute 를 전달하도록 CommandParameter호출됩니다.

메서드가 CanExecute 를 반환 true하면 컨트롤이 활성화되고, 그렇지 않으면 컨트롤이 사용하지 않도록 설정됩니다.

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 있는 개체CommandBinding를 찾는 요소 트리를 통해 터널 및 버블을 발생시키는 및 CanExecute 이벤트를 발생 PreviewCanExecute 합니다. CommandBinding 에 대한 가 RoutedCommand 발견 CanExecuteRoutedEventHandler 되면 에 연결된 CommandBinding 가 호출됩니다. 이러한 처리기는 가 실행될 수 있는지 여부를 결정하기 위한 프로그래밍 논리를 RoutedCommand 제공합니다.

PreviewCanExecutePreviewExecuted 이벤트는 에서 CommandTarget발생합니다. 가 CommandTargetICommandSourcePreviewCanExecute 설정되지 않은 경우 및 CanExecute 이벤트는 키보드 포커스가 있는 요소에서 발생합니다.

적용 대상