wpf - RoutedCommand and the CommandManager.

In previous discussion wpf - RoutedCommand use example - we have see the example of how to set up the CommandBindings and how to set the Command to an UIElement and how to create the Commands objects.

 

 

We also looked the the post - wpf - RoutedEvent and EventManager.RegisterClassHandler -  Routed Event Handlers (such as the class handlers and the instance handlers), you do that via the EventManager.RegisterClassHandler or through the UIElement.AddHandler methods;

 

you can always make comparsion between the CommandManager and the EventManager. While the following two are the CommandManager's equivalent on the RoutedEvent's AddHandlers (the RemoveHandlers is not discussed in this post).

 

 

CommandManager.AddCanExecuteHandler(UIElement, CanExecuteRoutedEventHandler);

CommandManager.AddexecutedHandler(UIElement, ExecutedRoutedEventHandler);

 

 

From what I am seeing, this is like to set up handler for the Command that is associated with the UI element (bypass the CommandBindings Stuff??)

 

 


The undetermined parts
 is the relation between the CommandBindings that belongs to the UIElement and the Handler that explicitly added to the UIElement through the use of CommandManager.AddExecutedHandler.

 

Now, there are some clear part about the relationship of the two - 

 

  • the CommandBindings always dominate the AddExecutedHandler. 

 

Let's see the example.

 

 

 

 

namespace RoutedCommandsTest
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      CommandManager.AddExecutedHandler(this.BtnHighLightCommand, OnHighlightCommandExecuted);
      CommandManager.AddCanExecuteHandler(this.BtnHighLightCommand, OnHighlightCommandCanExecuted);
      
      // now what if we add the CommandBindings for this button?
      // if you have the following statement running after the CommandManager.AddExecutedHandler, 
      // you will see that CommandBinding's Handler prevail
      this.BtnHighLightCommand.CommandBindings.Add(
        new CommandBinding(HighlightCommand,
          OnHighLightCommandBindingExecutedHandler,
          OnHighLightCommandBindingCanExecuteHandler));

      // even though you have the AddExecutedHandler added, you will still see the handler of 
      // CommandBindings invoked.
      CommandManager.AddExecutedHandler(this.BtnHighLightCommand, OnHighlightCommandExecuted);
      CommandManager.AddCanExecuteHandler(this.BtnHighLightCommand, OnHighlightCommandCanExecuted);
    }
    
    public static readonly RoutedCommand HighlightCommand = new RoutedCommand("HighlightCommand", typeof(MainWindow));


    public void OnHighlightCommandExecuted(object sender, ExecutedRoutedEventArgs e)
    {
      MessageBox.Show("this is the OnHighlightCommandExecuted Handler");
    }

    public void OnHighlightCommandCanExecuted(object sender, CanExecuteRoutedEventArgs e)
    {
      e.CanExecute = true;
    }

    public void OnHighLightCommandBindingExecutedHandler(object sender, ExecutedRoutedEventArgs e)
    {
      MessageBox.Show("This is the Command Binding's OnHighLightCommandExecute Handler");
      e.Handled = false;
    }

    public void OnHighLightCommandBindingCanExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
    {
      e.CanExecute = true;
    }

  }
}
 

 

and below is the xaml file 

 

 

<Window x:Class="RoutedCommandsTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:custom="clr-namespace:RoutedCommandsTest"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="BtnHighLightCommand" Content="HightLightCommand" Command="{x:Static custom:MainWindow.HighlightCommand}" />
    </Grid>
</Window>

你可能感兴趣的:(WPF)