방법: 명령을 지원하지 않는 컨트롤에 명령 후크

다음 예제는 RoutedCommand를 명령에 대한 지원을 기본 제공하지 않는 Control에 후크하는 방법을 보여줍니다. 여러 소스에 명령을 후크하는 전체 샘플은 Create a Custom RoutedCommand Sample(사용자 지정 RoutedCommand 만들기 샘플)을 참조하세요.

예제

WPF(Windows Presentation Foundation)는 애플리케이션 프로그래머가 자주 접하게 되는 공용 명령의 라이브러리를 제공합니다. 명령 라이브러리를 구성하는 클래스는 ApplicationCommands, ComponentCommands, NavigationCommands, MediaCommandsEditingCommands입니다.

이러한 클래스를 구성하는 정적 RoutedCommand 개체는 명령 논리를 제공하지 않습니다. 명령 논리는 CommandBinding이 있는 명령과 연결됩니다. WPF의 많은 컨트롤은 명령 라이브러리의 일부 명령에 대한 지원을 기본 제공합니다. 예를 들어 TextBoxPaste, Copy, Cut, RedoUndo 등의 많은 애플리케이션 편집 명령을 지원합니다. 애플리케이션 개발자는 이러한 명령을 컨트롤과 함께 사용하기 위해 별도로 해야 하는 작업은 없습니다. 명령이 실행될 때 TextBox가 명령 대상인 경우는 컨트롤에 내장되어 있는 CommandBinding을 사용하여 명령을 처리할 수 있습니다.

다음은 Open 명령에 대한 명령 소스로 Button을 사용하는 방법을 표시합니다. 지정된 CanExecuteRoutedEventHandlerCanExecuteRoutedEventHandlerRoutedCommand와 연결하는 CommandBinding을 만듭니다.

먼저, 명령 소스를 만듭니다. Button은 명령 소스로 사용됩니다.

<Button Command="ApplicationCommands.Open" Name="MyButton"
        Height="50" Width="200">
  Open (KeyBindings: Ctrl+R, Ctrl+0)
</Button>
// Button used to invoke the command
Button CommandButton = new Button();
CommandButton.Command = ApplicationCommands.Open;
CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)";
MainStackPanel.Children.Add(CommandButton);
' Button used to invoke the command
Dim CommandButton As New Button()
CommandButton.Command = ApplicationCommands.Open
CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)"
MainStackPanel.Children.Add(CommandButton)

다음으로, ExecutedRoutedEventHandlerCanExecuteRoutedEventHandler를 만듭니다. ExecutedRoutedEventHandler는 단순히 MessageBox를 열어 명령이 실행되었음을 나타냅니다. CanExecuteRoutedEventHandlerCanExecute 속성을 true로 설정합니다. 일반적으로 실행 가능 처리기는 현재 명령 대상에서 명령을 실행할 수 있는지 확인하려면 보다 강력한 확인 작업을 수행합니다.


void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
{
    String command, targetobj;
    command = ((RoutedCommand)e.Command).Name;
    targetobj = ((FrameworkElement)target).Name;
    MessageBox.Show("The " + command +  " command has been invoked on target object " + targetobj);
}
void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}


Private Sub OpenCmdExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    Dim command, targetobj As String
    command = CType(e.Command, RoutedCommand).Name
    targetobj = CType(sender, FrameworkElement).Name
    MessageBox.Show("The " + command + " command has been invoked on target object " + targetobj)
End Sub
Private Sub OpenCmdCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
    e.CanExecute = True
End Sub

마지막으로, CommandBinding은 라우팅된 이벤트 처리기를 Open 명령에 연결하는 애플리케이션의 Window 루트에 만들어집니다.

<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>
// Creating CommandBinding and attaching an Executed and CanExecute handler
CommandBinding OpenCmdBinding = new CommandBinding(
    ApplicationCommands.Open,
    OpenCmdExecuted,
    OpenCmdCanExecute);

this.CommandBindings.Add(OpenCmdBinding);
' Creating CommandBinding and attaching an Executed and CanExecute handler
Dim OpenCmdBinding As New CommandBinding(ApplicationCommands.Open, AddressOf OpenCmdExecuted, AddressOf OpenCmdCanExecute)

Me.CommandBindings.Add(OpenCmdBinding)

참고 항목