다음을 통해 공유


방법: RoutedCommand 만들기

이 예제는 ExecutedRoutedEventHandlerCanExecuteRoutedEventHandler를 만들고 CommandBinding에 연결하여 사용자 지정 명령을 구현하는 방법과 사용자 지정 RoutedCommand을 생성하는 방법을 보여 줍니다. 명령에 대 한 자세한 내용은 참조는 명령 개요합니다.

예시

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

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

애플리케이션에서 명령을 사용하려면 명령이 수행하는 작업을 정의하는 이벤트 처리기를 만들어야 합니다.

private void ExecutedCustomCommand(object sender,
    ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Custom Command Executed");
}
Private Sub ExecutedCustomCommand(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    MessageBox.Show("Custom Command Executed")
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;
    }
}
' 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

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

<Window x:Class="SDKSamples.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://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>
CommandBinding customCommandBinding = new CommandBinding(
    CustomRoutedCommand, ExecutedCustomCommand, CanExecuteCustomCommand);

// attach CommandBinding to root window
this.CommandBindings.Add(customCommandBinding);
Dim customCommandBinding As New CommandBinding(CustomRoutedCommand, AddressOf ExecutedCustomCommand, AddressOf CanExecuteCustomCommand)

' attach CommandBinding to root window
Me.CommandBindings.Add(customCommandBinding)

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

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

CustomCommandButton.Command = CustomRoutedCommand;
' create the ui
Dim CustomCommandStackPanel As New StackPanel()
Dim CustomCommandButton As New Button()
CustomCommandStackPanel.Children.Add(CustomCommandButton)

CustomCommandButton.Command = CustomRoutedCommand

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

참고하십시오