Jak podpiąć polecenie do formantu bez użycia obsługi poleceń

W poniższym przykładzie pokazano, jak podłączyć RoutedCommand element do elementu Control , który nie ma wbudowanej obsługi polecenia . Aby uzyskać kompletny przykład, który podłącza polecenia do wielu źródeł, zobacz przykład Create a Custom RoutedCommand Sample (Tworzenie niestandardowego routedCommand Sample ).

Przykład

Program Windows Presentation Foundation (WPF) udostępnia bibliotekę typowych poleceń, które programisty aplikacji napotykają regularnie. Klasy składające się z biblioteki poleceń to: ApplicationCommands, , NavigationCommandsComponentCommands, MediaCommands, i EditingCommands.

Obiekty statyczne RoutedCommand tworzące te klasy nie dostarczają logiki poleceń. Logika polecenia jest skojarzona z poleceniem za pomocą polecenia CommandBinding. Wiele kontrolek w WPF ma wbudowaną obsługę niektórych poleceń w bibliotece poleceń. TextBox, na przykład obsługuje wiele poleceń edycji aplikacji, takich jak Paste, Copy, Cut, Redoi Undo. Deweloper aplikacji nie musi wykonywać żadnych specjalnych czynności, aby te polecenia działały z tymi kontrolkami. TextBox Jeśli element jest obiektem docelowym polecenia, gdy polecenie jest wykonywane, będzie obsługiwać polecenie przy użyciu CommandBinding wbudowanej kontrolki.

Poniżej pokazano, jak użyć Button elementu jako źródła poleceń dla Open polecenia . Zostanie CommandBinding utworzony obiekt, który kojarzy określony CanExecuteRoutedEventHandler element i element CanExecuteRoutedEventHandler z elementem RoutedCommand.

Najpierw jest tworzone źródło polecenia. Element A Button jest używany jako źródło polecenia.

<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)

Następnie zostaną utworzone wartości ExecutedRoutedEventHandler i CanExecuteRoutedEventHandler . Po ExecutedRoutedEventHandler prostu otwiera element MessageBox , aby oznaczyć, że wykonane polecenie. Właściwość CanExecuteRoutedEventHandler ustawia CanExecute wartość true. Zwykle program obsługi może wykonywać bardziej niezawodne kontrole, aby sprawdzić, czy polecenie może zostać wykonane na bieżącym obiekcie docelowym polecenia.


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

Na koniec tworzony jest element CommandBinding w katalogu głównym Window aplikacji, która kojarzy programy obsługi zdarzeń kierowanych do Open polecenia.

<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)

Zobacz też