다음을 통해 공유


방법: RoutedCommand 만들기

이 예제에서는 사용자 지정 RoutedCommand를 만드는 방법과 ExecutedRoutedEventHandlerCanExecuteRoutedEventHandler를 만든 다음 CommandBinding에 연결하여 사용자 지정 명령을 구현하는 방법을 보여 줍니다. 명령에 대한 자세한 내용은 명령 개요를 참조하십시오.

예제

RoutedCommand를 만드는 첫 번째 단계는 명령을 정의하고 인스턴스화하는 단계입니다.

        Public Shared CustomRoutedCommand As New RoutedCommand()
public static RoutedCommand CustomRoutedCommand = new RoutedCommand();

응용 프로그램에서 명령을 사용하려면 명령이 수행할 작업을 정의하는 이벤트 처리기를 만들어야 합니다.

        Private Sub ExecutedCustomCommand(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
            MessageBox.Show("Custom Command Executed")
        End Sub
private void ExecutedCustomCommand(object sender,
    ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Custom Command Executed");
}
        ' CanExecuteRoutedEventHandler that only returns true if
        ' the source is a control.
        Private Sub CanExecuteCustomCommand(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
            Dim target As Control = TryCast(e.Source, Control)

            If target IsNot Nothing Then
                e.CanExecute = True
            Else
                e.CanExecute = False
            End If
        End Sub
// CanExecuteRoutedEventHandler that only returns true if
// the source is a control.
private void CanExecuteCustomCommand(object sender, 
    CanExecuteRoutedEventArgs e)
{
    Control target = e.Source as Control;

    if(target != null)
    {
        e.CanExecute = true;
    }
    else
    {
        e.CanExecute = false;
    }
}

그런 다음 명령을 이벤트 처리기와 연결하는 CommandBinding을 만듭니다. CommandBinding은 특정 개체에서 만들어집니다. 이 개체는 요소 트리에서 CommandBinding의 범위를 정의합니다.

<Window x:Class="SDKSamples.Window1"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:SDKSamples"
    Height="600" Width="800"
    >
  <Window.CommandBindings>
    <CommandBinding Command="{x:Static custom:Window1.CustomRoutedCommand}"
                    Executed="ExecutedCustomCommand"
                    CanExecute="CanExecuteCustomCommand" />
  </Window.CommandBindings>
            Dim customCommandBinding As New CommandBinding(CustomRoutedCommand, AddressOf ExecutedCustomCommand, AddressOf CanExecuteCustomCommand)

            ' attach CommandBinding to root window
            Me.CommandBindings.Add(customCommandBinding)
CommandBinding customCommandBinding = new CommandBinding(
    CustomRoutedCommand, ExecutedCustomCommand, CanExecuteCustomCommand);

// attach CommandBinding to root window
this.CommandBindings.Add(customCommandBinding);

마지막 단계에서는 명령을 호출합니다. 명령을 호출하는 한 가지 방법은 명령을 Button과 같은 ICommandSource와 연결하는 것입니다.

<StackPanel>
  <Button Command="{x:Static custom:Window1.CustomRoutedCommand}"
          Content="CustomRoutedCommand"/>
</StackPanel>
            ' create the ui
            Dim CustomCommandStackPanel As New StackPanel()
            Dim CustomCommandButton As New Button()
            CustomCommandStackPanel.Children.Add(CustomCommandButton)

            CustomCommandButton.Command = CustomRoutedCommand
// create the ui
StackPanel CustomCommandStackPanel = new StackPanel();
Button CustomCommandButton = new Button();
CustomCommandStackPanel.Children.Add(CustomCommandButton);

CustomCommandButton.Command = CustomRoutedCommand;

단추를 클릭하면 사용자 지정 RoutedCommand에서 Execute 메서드가 호출됩니다. RoutedCommand에서는 PreviewExecutedExecuted 라우트된 이벤트를 발생시킵니다. 이러한 이벤트는 해당 특정 명령에 대한 CommandBinding을 찾아 요소 트리를 이동합니다. CommandBinding이 발견되면 CommandBinding과 연결된 ExecutedRoutedEventHandler가 호출됩니다.

참고 항목

참조

RoutedCommand

개념

명령 개요