다음을 통해 공유


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

특성

예외

아닌 경우

예제

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

this.Command 이 예제에서는 .의 Command 속성입니다 ICommandSource. 명령이 아닌 null경우 명령은 .로 RoutedCommand캐스팅됩니다. 명령이 이 RoutedCommand면 메서드를 CanExecute 전달하여 메서드를 CommandTarget 호출합니다 CommandParameter. 명령이 RoutedCommand아닌 경우 명령이 캐스팅되고 ICommand 메서드를 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 가 있는 개체를 찾는 요소 트리를 통해 터널 및 버블을 발생 PreviewCanExecute 시키는 이벤트 및 CanExecuteCommandBinding발생합니다. for가 CommandBindingRoutedCommand 발견 CanExecuteRoutedEventHandler 되면 연결된 CommandBinding 값이 호출됩니다. 이러한 처리기는 실행할 수 있는지 여부를 확인하기 위한 프로그래밍 논리를 RoutedCommand 제공합니다.

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

적용 대상