Compartilhar via


Como: Enganchar um Comando em um Controle sem Suporte de Comando

The following example shows how to hook up a RoutedCommand to a Control which does not have built in support for the command. Para obter um exemplo completo que conecta os comandos de várias fontes, consulte o criar uma amostra de RoutedCommand personalizado exemplo.

Exemplo

Windows Presentation Foundation (WPF) provides a library of common commands which application programmers encounter regularly. As classes que compõem a biblioteca de comando são: ApplicationCommands, ComponentCommands, NavigationCommands, MediaCommands, and EditingCommands.

The static RoutedCommand objects which make up these classes do not supply command logic. The logic for the command is associated with the command with a CommandBinding. Many controls in WPF have built in support for some of the commands in the command library. TextBox, for example, supports many of the application edit commands such as Paste, Copy, Cut, Redo, and Undo. The application developer does not have to do anything special to get these commands to work with these controls. If the TextBox is the command target when the command is executed, it will handle the command using the CommandBinding that is built into the control.

The following shows how to use a Button as the command source for the Open command. A CommandBinding is created that associates the specified CanExecuteRoutedEventHandler and the CanExecuteRoutedEventHandler with the RoutedCommand.

First, the command source is created. A Button is used as the command source.

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

Next, the ExecutedRoutedEventHandler and the CanExecuteRoutedEventHandler are created. O ExecutedRoutedEventHandler simplesmente abre uma MessageBox para indicar que o comando é executado. The CanExecuteRoutedEventHandler sets the CanExecute property to true. Normally, the can execute handler would perform more robust checks to see if the command could execute on the current command target.


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

Finally, a CommandBinding is created on the root Window of the application that associates the routed events handlers to the Open command.

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

Consulte também

Tarefas

Como: Hook Up a Command to a Control with Command Support

Conceitos

Visão geral de Comando