AttachedCommandBehavior V2 aka ACB

YOUAfter publishing my AttachedCommandBehavior library, (you can read about this here), some WPF Disciples recommended some new features to make the library cover more use cases. Thanks William Kempf your input was amazing!

I added 2 new features

– Support for Collection of binding to commands
– Support for Light weight Commands or as I call them Action

 

Support for Collection of binding to commands

This is a very handy feature. Let’s say you want to hook a command to the MouseDown but also want to hook up to the MouseEnter event, in ACB v1 you could now do this because you could only Bind one command at a time. Now the ACB v2 is supporting a collection of what I call BehaviorBinding. This is really powerful because it enables you to hook up to N number of events. The code would look something like this

   1: <Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" x:Name="test">
   2:     <local:CommandBehaviorCollection.Behaviors>
   3:             <local:BehaviorBinding Event="MouseLeftButtonDown" Action="{Binding DoSomething}" CommandParameter="An Action on MouseLeftButtonDown"/>
   4:             <local:BehaviorBinding Event="MouseRightButtonDown" Command="{Binding SomeCommand}" CommandParameter="A Command on MouseRightButtonDown"/>
   5:     </local:CommandBehaviorCollection.Behaviors>
   6:     <TextBlock Text="MouseDown on this border to execute the command"/>
   7: </Border>

So basically there is a read-only attached property Behaviors (which by the way it is something really cool, have a look at this post to find out more) and here you can define as many BehaviorBindings as you wish. I really love this part because I had to do some WPF tricks like the Freezable trick to make DataBinding work even in the Behaviors collections.

 

Support for Light weight Commands or as I call them Action

This is something really cool as well. Me and the Disciples were discussing how this could be a way to work around the fact that you cannot have a command handler in the XAML pointing to a method in the ViewModel, then William Kempf said

“Commands imply, to
me at least, CanExecute functionality.  Once we extend this idea to
embrace multiple events, CanExecute functionality starts to become a
little murky, at best.  Without CanExecute, you may just as well be
dealing with a delegate instead of an ICommand.”

Read the full thread here

So I decided to add support for this. Basically this feature enables you to expose a Delegate (of type Action<object> ) as a property in the ViewModel which would be called when the specified event is raised. Something like this

   1: <Border Background="DarkSalmon" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" 
   2:         local:CommandBehavior.Event="MouseDown"  
   3:         local:CommandBehavior.Action="{Binding DoSomething}"
   4:         local:CommandBehavior.CommandParameter="from the DarkSalmon Border :P"/>

and the ViewModel property would look like this

   1: DoSomething = x => Messages.Add("Action executed: " + x.ToString());

As you can see in the example you can still use the CommandParameter attached property and it will be passed to you when the delegate is called.

 

Conclusion

I think that these 2 features add a lot to the ACB. I quite happy with what I achieved here and I hope that this library will also help you. Any feedback comments bug reports are most welcome 🙂

WARNING: This code was not tested a lot so expect bugs etc… If you want to use this in production code you are doing so at your own risk 😛    yet I must say it works on my machine …. hehe

DOWNLOAD AttachedCommandBehavior v2.0

kick it on DotNetKicks.com

Advertisements