CommandBinding.Command Propriedade

Definição

Obtém ou define o ICommand associado a esse CommandBinding.

C#
[System.Windows.Localizability(System.Windows.LocalizationCategory.NeverLocalize)]
public System.Windows.Input.ICommand Command { get; set; }

Valor da propriedade

O comando associado a essa associação.

Atributos

Exemplos

O exemplo a seguir cria um CommandBinding que mapeia um ExecutedRoutedEventHandler e um CanExecuteRoutedEventArgs manipulador para o Open comando .

XAML
<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>
C#
// Creating CommandBinding and attaching an Executed and CanExecute handler
CommandBinding OpenCmdBinding = new CommandBinding(
    ApplicationCommands.Open,
    OpenCmdExecuted,
    OpenCmdCanExecute);

this.CommandBindings.Add(OpenCmdBinding);

A seguir, mostra o CanExecuteRoutedEventHandler que define CanExecute como true.

C#
void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}

A seguir, mostra o ExecutedRoutedEventHandler que cria um MessageBox quando o comando é executado.

C#
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);
}

Aplica-se a

Produto Versões
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Confira também