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.”
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"/>
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





Cool dude
– This can be VERY useful – ACB is much more muscular now!!
[...] Marlon Grech on AttachedCommandBehavior V2 aka ACB [...]
Pingback by 2008 December 15 - Links for today « My (almost) Daily Links | December 16, 2008 |
I’m wondering if there’s a way to get the control to disable or enable when using either an action or command binding?
In the sample there is a SimpleCommand “ClearMessagesCommand” that is bound to a button the “usual” way:
But I would like to be able to bind the command to other events on the button like MouseDown / MouseOver etc. AND have the control disable.
Is there a way to extract the “CanExecute” return value like this:
<Button IsEnabled=”{Binding
ClearMessagesCommand.CanExecute}”
Next version of ACB will have a lot of these things
just give me some time
[...] my purposes. I must credit Mike Hillberg and his MethodCommand solution and Marlon Grech and his Attached Command Behaviors solution for guidance and inspiration as I waded into [...]
Pingback by MethodCaller: Invoking Methods betwen the View and the ViewModel « My Development KB | February 14, 2009 |
[...] 2 http://groups.google.com/group/wpf-disciples/browse_thread/thread/63cbe8b2b0575e4e?pli=1 3: http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/ a smart way to use double click [...]
Pingback by attached property « Steveluo’s Blog | February 24, 2009 |
[...] some time and build a WPF sample application to showcase the use of MVVM + Mediator and also how AttachedCommandBehaviours can be used to further reduce (or better eliminate) C# code in XAML code behind files. I am also [...]
Pingback by [MVVM + Mediator + ACB = cool WPF App] – Intro « C# Disciples | April 2, 2009 |
Hi Marlon,
I recently study your ACB.
it is a very good idea.
and it also my problems.
but,…
I found a case fails ACB.
it is about routed command and foucs.
I use ACB on a border and assign a command react on boder’s MouseDown event.
If the focus is not on the view of this border, the command can not route to it’s view, but can route to it’s parent view.
Do you have any idea about this issue, thank you.
I didn’t quite understand … can you re explain please
Hi Marlon
There is a sample code for my issue,
http://caa.myweb.hinet.net\MyACBSample.zip
There are only one window and one view in this project,and
this view only contains one button and one border.
If the focus not on viewButton,
“Mouse Down” on border1 can not execute command.
but after click viewButton, it works(pop a message).
I also try to listen MouseDown event of border1 and execute command in the event handler in code behind, it works too.
I don`t realize why ACB Command could not work in this case.
Thanks for your comment, and this is a much useful method.
I don’t think this has anything to do with ACB it just how the routed events work…
Try using PreviewMouseDown. I think that would work for you.
P.S couldn’t download the file you sent… was saying Error 404
[...] http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/ [...]
Pingback by [WPF] Comment actionner une commande WPF lorsqu’un évènement est déclenché ? , Thomas Lebrun | April 14, 2009 |
Great solution, but if Event that i need to route send for me EventArgs?
Can i bind event “EventArgs” to CommandParameter? And how can i do that?
not with the current implementation… sorry
Well, maybe you give me some tricks for modify EventHandlerGenerator.CreateDelegate method to support CommandBehaviorBinding.Execute with parameters? I’m not full understand ILGeneration there, but you are read all parameters from eventHandlerInfo, so may be this parameters can be binded to some parameters in Execute method?
For example:
public void Execute(object parameter)
{
if(CommandParameter == null)
{
strategy.Execute(parameter);
}
else
{
strategy.Execute(CommandParameter);
}
}
where “object parameter” is event EventArgs…
I’ll have to look into this…. because it is not that easy….
Thanks! This will be great, if it possible
[...] should state that this post was inspired by a good friend of mine Marlon Grechs AttachedCommandBehavior V2 aka ACB blog post. I loved and have used Marlons idea, but he uses dynamically created IL, to create the [...]
Pingback by sachabarber.net » WPF : Attached Commands | May 2, 2009 |
Damn, yeah! I’m merge ACB V2 and info from sachabarber.net, and this is it! Now i can bind command parameter, or hook EventArg’s! Thnx so much!
What i’m change:
1) Delete EventHandlerGenerator class.
2) Delete CommandBehaviorBinding.Execute method.
3) Update CommandBehaviorBinding.BindEvent method to this:
public void BindEvent(DependencyObject owner, string eventName)
{
EventName = eventName;
Owner = owner;
Event = Owner.GetType().GetEvent(EventName, BindingFlags.Public | BindingFlags.Instance);
if (Event == null)
throw new InvalidOperationException(String.Format(“Could not resolve event name {0}”, EventName));
//Register the handler to the Event
EventHandler = Delegate.CreateDelegate(Event.EventHandlerType, this,
GetType().GetMethod(“OnEventRaised”,
BindingFlags.NonPublic |
BindingFlags.Instance));
Event.AddEventHandler(Owner, EventHandler);
}
4) Add OnEventRaised method to CommandBehaviorBinding class:
///
/// Runs the ICommand when the requested RoutedEvent fires
///
private void OnEventRaised(object sender, EventArgs e)
{
if (Command != null)
{
if(CommandParameter != null)
{
Command.Execute(CommandParameter);
}
else
{
Command.Execute(e);
}
}
}
5) Enjoy and have fun
it is really greate!!
I saw your idea and clever implementation of it.
Thank you~~ ^^
And little more… If we bind CommandParameter to some item. And on some event we need to take this item or null value? So, with this implementation we will take binded item, or event args… This method is little wrong
I resolve this problem like this:
1) In CommandBehaviorBinding replace CommandParameter property to this:
///
/// Gets or sets a CommandParameter
///
private object commandParameter;
public object CommandParameter
{
get
{
return commandParameter;
}
set
{
commandParameter = value;
IsCommandParameterBinded = true;
}
}
2) Add IsCommandParameterBinded property to CommandBehaviorBinding:
protected bool IsCommandParameterBinded { get; set; }
3) Update OnEventRaised method in CommandBehaviorBinding to this:
///
/// Runs the ICommand when the requested RoutedEvent fires
///
private void OnEventRaised(object sender, EventArgs e)
{
if (strategy != null)
{
if (IsCommandParameterBinded)
{
strategy.Execute(CommandParameter);
}
else
{
strategy.Execute(e);
}
}
}
So now, if we bind CommandParameter we will take only CommandParameter values. If not, we will hook EventArg’s. I’m not sure about replacing “Command” to “strategy” there, but I’m do that.
Marlon, may be you collect this changes to next version and fix our misunderstanding about Command and strategy in CommandBehaviorBinding?
[...] http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/ [...]
Pingback by kevin Mocha - AttachedCommandBehavior V2 aka ACB | May 29, 2009 |
visit us!
newsbox.cc
newsbox.us
nbstatus.wordpress.com
NOW!
Hi, very very nice article. I love it since it helps me handling events and commands in a consistent manner. Thers only a question: Can I get rid of the Error in the XAML editor about the .Behaviours attached property that is not found on the type CommandBehaviorCollection? Thanks for your work, its GREAT!
Hi, very good article, I have used your sample for a mouseDown and get my commnad to be executed on the ViewModel. How do I get the mouse position to be passed to the ViewModel
TIA
Yaz
you can get the event args like this
http://marlongrech.wordpress.com/2009/07/03/how-to-get-the-eventargs-as-a-commandparameter-using-the-attachedcommandbehaviour/
Can anyone tell me how to use it in a Style?
Hi Marlongrech
I have been using your idea of ACB v1 to develop an attached behavior which is able to support hooking multiple event on a certain WPF control and now I have to deal with a strange issue which I don’t know why.
My source code was uploaded at
http://www.yousendit.com/download/Z01OTXRhbEoxUUJjR0E9PQ
The following xaml code is where the strange happening. The issue is I was only able to get one eventName “PreviewMouseDown” the last one in the following code.
But what i expect is the xamp parser should send to me both of them. If you have a free time could you please take a quick look and help me to answer why. and how to ask XAMLParser sending both to the code behide
Hi Marlon,
I’d like to know how to get rid of the error in the XAML editor when using CommandBehaviorCollection.Behaviours.
Thanks
Ian
Great.. ;D…
Was looking for the same stuff for Silverlight ;D… this is realy nice…
Thanks,
Akhil
Ian,
I was running into the same problem. Using the Visual Studio debugger, I tracked it down to the CommandBehavior.OnEventChanged method, where the e.NewValue is apparently null (and attempting to call ToString() on it). I just wrapped a null check around it and now it loads in the XAML designer view in both VS and Blend.
I like this solution and am contemplating using it, but want to look it over for such error handling first. Thanks, Marlon for a nice solution using the attached behavior pattern.
Greg
Doh! I think I just caused more problems. I’ll get back to you on that one…
Nope. That check was OK. The “problems” mentioned above were from some other experimental code.
Hi, Marlon!
I’m researching Attached command behavior and trying to compare your solution and this solution http://chris.59north.com/post/The-CommandManager-again.aspx
So you use IL for eventhandler generator, while other solution – doesn’t. Which makes it possible (at first glance and superficial testing) for using in SilverLight applications. Can You say why have You used IL? What advantages does it have? My knowledge in C# isn’t so deep so I can’t do it by myself. And I’ll appreciate your help!
because its the only way to generate event handlers at runtime…
There is another approach that uses the EventManager but it works only for routed events
Actually if the events you are trying to bind to follow the (object sender, EventArgs args) signature you can call Delegate.CreateDelegate for Event.EventHandlerType. All of the events on the WPF controls follow this signature so it shouldn’t be a problem.
This is how I get around the problem of not being able to generate IL in partial trust (We are writing an XBAP).
There is still the problem of VS 2008 complaining about the Behaviors collection but that is another matter.
that’s a very good point… nice idea dude!
Just to let you know marlon I think ACB is a great idea and fills a very real need when doing MVVM. As its been said before, “Some times you just need to get the event args”.
Plus there are a couple of cases where i want to take action off of an event that aren’t linked to any specific Command.
So thanks a bunch.
I am getting this error in xaml
The attachable property ‘Behaviors’ was not found in type ‘CommandBehaviorCollection’.
Code runs fine. But I can’t viw design screen.
Can you please let me know how this can be corrected?
thats a n issue with Cider the Designer for VS… hopefully it is fixed in VS 2010
First let me express my many thanks to you for the ACB!
Here is my fix regarding the Visual Studio Designer issue (I use VS2008):
///
/// Gets the Behaviors property.
/// Here we initialze the collection and set the Owner property
///
public static BehaviorBindingCollection GetBehaviors(DependencyObject d)
{
if (d == null)
throw new InvalidOperationException(“The dependency object trying to attach to is set to null”);
BehaviorBindingCollection collection = d.GetValue(BehaviorsProperty) as BehaviorBindingCollection;
if (collection == null)
{
collection = new BehaviorBindingCollection();
collection.Owner = d;
SetBehaviorsPrivately(d, collection);
}
return collection;
}
///
/// Just keep the definition of the attachable property ‘Behaviors’ intact
/// to allow Visual Studio designer to show properly instead of throwing the error:
/// The attachable property ‘Behaviors’ was not found in type ‘CommandBehaviorCollection’.
/// Actually this definition is not used at all at the run time.
///
public static void SetBehaviors(DependencyObject d, BehaviorBindingCollection value)
{
}
///
/// Provides a secure (private) method for setting the Behaviors property.
/// The main purpose here is to hook up the CollectionChanged event hanlder.
///
private static void SetBehaviorsPrivately(DependencyObject d, BehaviorBindingCollection value)
{
d.SetValue(BehaviorsPropertyKey, value);
INotifyCollectionChanged collection = (INotifyCollectionChanged)value;
collection.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChanged);
}
But after fixed this, I got another error:
Property ‘Action’ was not found or is not serializable for type ‘Border’.
The problem I believ it is reated to Action type returned by GetAction method. I could do any one of the flowing three to fix the problem:
1. Set the method as non-public;
2. Change the method name;
3. Get rid of this method;
This fix is good both at the design time and run time. But I am confused by the presence of GetAction method.
I tested it the other day in VS 2010 Beta 2 and it worked so unless they do something completely stupid done between now and when they release ….
Got it.. Thanks Marlon for this wonderful concept.
Hi Marlon,
could you please elaborate a little on the Behavior hack?
You simply write “it’s a trick to get it work” in the comment for the BehaviorsPropertyKey a.k.a as BehaviorsInternal.
But could you tell me why it does work then?
Some for the “hack” with the freezable collection and so on. I can see that it really only works with the freezable… but why?
Besides, does really nobody have a clue why the Cider designer does not work with the behavior collection? Its really anoying.
But anyhow, great work Marlon, I love this behavior.
Hi, nice work!
But I’m having a problem, I’m trying to fire MouseLeftButtonUp as a Command but I always get null exception at CommandBehaviorBinding line 99. It seems that strategy is null. Any thoughts?
It also happens with Actions.
André Carlucci
Go it… so silly, it was a binding problem.
Cheers.
Loving this… the part where you generate the eventhandlers seems like magic
…
I want to use it in a project, but just to be sure what is the license of it?
Thanks Marlon, this is great. Only one question, and I may be asking too much – if I use an AttachedCommandBehavior to catch the PreviewKeyDown event, how can I get the actual key pressed in the command handler? Thanks again.
Hi Marlon, nice work!
But how to use in styles?
This code will not work:
…
…
Best regards Kays
Was looking for the same stuff for Silverlight ;D… this is realy nice…
Comment by Admin February 23, 2010 @ 6:50pm
hello, if its not too much of a problem could you take a look at a problem I am having.
I described it here, with source pls take a look:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/53527ef7-97b5-41ec-bce0-f356312a6f4f
When using CommandBehavior, is any way to cancel an event? For instance, the Closing event, I would like to set CancelEventArg = true, if it is not ready to close yet.
Thanks!
Hehe I am really the only reply to your incredible read?
strategy is always null. How do I fix this
[...] для параметров. Одна из реализаций описана в статье AttachedCommandBehavior V2 aka ACB. Автор разработал библиотеку, позволяющую вызывать [...]
Pingback by MVVM подход к обработке события Click в ItemsControl - CodeHelper | July 15, 2010 |
I have a static command reference in a HierarchicalDataTemplate. It doesn’t seem to run the canexecute until the button is pressed
<HierarchicalDataTemplate
…
<Button
acb:CommandBehavior.Command="{x:Static vm:StiRosterExplorerViewModel.EditCommand}"
acb:CommandBehavior.Event="Click"
acb:CommandBehavior.CommandParameter="{Binding}"
…
am i missing something?
Hi,
I love things that you’ve done.
Well, I have two questions:
1. Is there new version of ACB (v.3 ?:)) ?
2. I can’t run v.2 example for CommandBehaviorCollection
…
there is no such things as Behaviors.
can you correct your example ?
Thank you,
Julian
Auto message: notify me when answer will be here
I’d like to add some detailed info to me question:
The attachable property ‘Behaviors’ was not found in type ‘CommandBehaviorCollection’
ups, it works, sorry for spam
glad it worked… with regards to V3 not sure… don’t really have anything else to add to it for now… did you have anything in mind?