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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值