Share via


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

다음 예제에서는 명령을 기본적으로 지원하지 않는 ControlRoutedCommand를 후크하는 방법을 보여 줍니다. 여러 소스에 명령을 후크하는 방법을 보여 주는 전체 샘플은 Create a Custom RoutedCommand 샘플을 참조하십시오.

예제

Windows Presentation Foundation (WPF)은 응용 프로그램 프로그래머가 흔히 사용하는 일반 명령이 포함된 라이브러리를 제공합니다. 이 명령 라이브러리를 구성하는 클래스는 ApplicationCommands, ComponentCommands, NavigationCommands, MediaCommandsEditingCommands입니다.

이러한 클래스를 구성하는 정적 RoutedCommand 개체는 명령 논리를 제공하지 않습니다. 명령 논리는 CommandBinding을 통해 명령에 연결됩니다. WPF에서 제공하는 대부분의 컨트롤은 명령 라이브러리의 일부 명령을 기본적으로 지원합니다. 예를 들어 TextBoxPaste, Copy, Cut, RedoUndo와 같은 대부분의 응용 프로그램 편집 명령을 지원합니다. 따라서 응용 프로그램 개발자는 컨트롤에서 이러한 명령이 작동하도록 하기 위해 특별한 작업을 수행할 필요가 없습니다. 명령이 실행될 때 명령 대상이 TextBox인 경우 컨트롤에 기본적으로 제공된 CommandBinding을 사용하여 명령을 처리합니다.

다음 예제에서는 ButtonOpen 명령의 명령 소스로 사용하는 방법을 보여 줍니다. 지정된 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
            Dim CommandButton As New Button()
            CommandButton.Command = ApplicationCommands.Open
            CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)"
            MainStackPanel.Children.Add(CommandButton)
// 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);

그런 다음 ExecutedRoutedEventHandlerCanExecuteRoutedEventHandler를 만듭니다. ExecutedRoutedEventHandler는 단순히 MessageBox를 열어 명령이 실행되었음을 보여 줍니다. CanExecuteRoutedEventHandlerCanExecute 속성을 true로 설정합니다. 일반적으로 CanExecute 처리기는 현재 명령 대상에서 명령을 실행할 수 있는지 여부를 확인하기 위해 보다 강력한 확인 작업을 수행합니다.


        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


        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;
        }

마지막으로, 라우트된 이벤트 처리기를 Open 명령에 연결하는 응용 프로그램의 루트 WindowCommandBinding을 만듭니다.

<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>
            ' Creating CommandBinding and attaching an Executed and CanExecute handler
            Dim OpenCmdBinding As New CommandBinding(ApplicationCommands.Open, AddressOf OpenCmdExecuted, AddressOf OpenCmdCanExecute)

            Me.CommandBindings.Add(OpenCmdBinding)
// Creating CommandBinding and attaching an Executed and CanExecute handler
CommandBinding OpenCmdBinding = new CommandBinding(
    ApplicationCommands.Open,
    OpenCmdExecuted,
    OpenCmdCanExecute);

this.CommandBindings.Add(OpenCmdBinding);

참고 항목

작업

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

개념

명령 개요