C# Disciples

my life in Avalon ….

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 :P     yet I must say it works on my machine …. hehe

DOWNLOAD AttachedCommandBehavior v2.0

kick it on DotNetKicks.com

About these ads

December 13, 2008 - Posted by | WPF

97 Comments »

  1. Cool dude :D – This can be VERY useful – ACB is much more muscular now!!

    Comment by Jonathan | December 15, 2008 | Reply

  2. [...] Marlon Grech on AttachedCommandBehavior V2 aka ACB [...]

    Pingback by 2008 December 15 - Links for today « My (almost) Daily Links | December 16, 2008 | Reply

  3. 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}”

    Comment by Brett | February 12, 2009 | Reply

  4. Next version of ACB will have a lot of these things :) just give me some time :)

    Comment by Marlon Grech | February 12, 2009 | Reply

  5. [...] 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 | Reply

  6. [...] 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 | Reply

  7. 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.

    Comment by Jostein | April 10, 2009 | Reply

    • I didn’t quite understand … can you re explain please

      Comment by marlongrech | April 10, 2009 | Reply

  8. 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.

    Comment by Jostein | April 13, 2009 | Reply

  9. 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

    Comment by marlongrech | April 13, 2009 | Reply

  10. 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?

    Comment by Settler | April 16, 2009 | Reply

  11. not with the current implementation… sorry

    Comment by marlongrech | April 16, 2009 | Reply

  12. 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…

    Comment by Settler | April 17, 2009 | Reply

  13. I’ll have to look into this…. because it is not that easy….

    Comment by marlongrech | April 17, 2009 | Reply

  14. Thanks! This will be great, if it possible :)

    Comment by Settler | April 17, 2009 | Reply

  15. [...] 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 | Reply

  16. 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 ;)

    Comment by Settler | May 18, 2009 | Reply

    • In this example your are not executing the strategy. You will then miss out the check for CanExecute.

      Comment by Matte | January 2, 2012 | Reply

  17. it is really greate!!

    I saw your idea and clever implementation of it.

    Thank you~~ ^^

    Comment by illef | May 19, 2009 | Reply

  18. 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?

    Comment by Settler | May 19, 2009 | Reply

  19. visit us!
    newsbox.cc
    newsbox.us
    nbstatus.wordpress.com
    NOW!

    Comment by GetUnfannamer | June 12, 2009 | Reply

  20. 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!

    Comment by Yannik | July 20, 2009 | Reply

  21. 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

    Comment by Yazid | July 24, 2009 | Reply

  22. Can anyone tell me how to use it in a Style?

    Comment by frostred | October 12, 2009 | Reply

  23. 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

    Comment by nguyennx | October 13, 2009 | Reply

  24. Hi Marlon,

    I’d like to know how to get rid of the error in the XAML editor when using CommandBehaviorCollection.Behaviours.

    Thanks
    Ian

    Comment by Ian Johnson | October 15, 2009 | Reply

  25. Great.. ;D…

    Was looking for the same stuff for Silverlight ;D… this is realy nice…

    Thanks,
    Akhil

    Comment by Akhil | October 27, 2009 | Reply

  26. 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

    Comment by Greg | November 3, 2009 | Reply

  27. Doh! I think I just caused more problems. I’ll get back to you on that one…

    Comment by Greg | November 3, 2009 | Reply

  28. Nope. That check was OK. The “problems” mentioned above were from some other experimental code.

    Comment by Greg | November 4, 2009 | Reply

  29. 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!

    Comment by wazzzuup | November 15, 2009 | Reply

  30. 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

    Comment by marlongrech | November 17, 2009 | Reply

    • 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.

      Comment by Ian Johnson | November 17, 2009 | Reply

  31. that’s a very good point… nice idea dude!

    Comment by marlongrech | November 17, 2009 | Reply

  32. 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.

    Comment by Ian Johnson | November 17, 2009 | Reply

  33. 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?

    Comment by Sukh | November 17, 2009 | Reply

  34. thats a n issue with Cider the Designer for VS… hopefully it is fixed in VS 2010

    Comment by marlongrech | November 18, 2009 | Reply

    • 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.

      Comment by Tao Lin | April 29, 2010 | Reply

  35. 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 ….

    Comment by Ian Johnson | November 18, 2009 | Reply

  36. Got it.. Thanks Marlon for this wonderful concept.

    Comment by Sukh | November 18, 2009 | Reply

  37. 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. :)

    Comment by towelie | November 19, 2009 | Reply

  38. 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

    Comment by André Carlucci | December 4, 2009 | Reply

  39. Go it… so silly, it was a binding problem.

    Cheers.

    Comment by André Carlucci | December 4, 2009 | Reply

  40. 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?

    Comment by somnambulist | December 16, 2009 | Reply

  41. 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.

    Comment by daggmano | January 15, 2010 | Reply

  42. Hi Marlon, nice work!
    But how to use in styles?

    This code will not work:

    Best regards Kays

    Comment by Kay | February 11, 2010 | Reply

  43. Was looking for the same stuff for Silverlight ;D… this is realy nice…

    Comment by Admin February 23, 2010 @ 6:50pm

    Comment by sustainabilityblogger | February 23, 2010 | Reply

  44. 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

    Comment by bbuddy | February 26, 2010 | Reply

  45. 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!

    Comment by Amy | May 4, 2010 | Reply

  46. Hehe I am really the only reply to your incredible read?

    Comment by Albert Espinoza | May 27, 2010 | Reply

  47. strategy is always null. How do I fix this

    Comment by Elbino | May 27, 2010 | Reply

  48. [...] для параметров. Одна из реализаций описана в статье AttachedCommandBehavior V2 aka ACB. Автор разработал библиотеку, позволяющую вызывать [...]

    Pingback by MVVM подход к обработке события Click в ItemsControl - CodeHelper | July 15, 2010 | Reply

  49. 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?

    Comment by segfault | August 17, 2010 | Reply

  50. 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

    Comment by Julian Ustiyanovych | August 30, 2010 | Reply

  51. Auto message: notify me when answer will be here :)

    Comment by Julian Ustiyanovych | August 30, 2010 | Reply

  52. I’d like to add some detailed info to me question:

    The attachable property ‘Behaviors’ was not found in type ‘CommandBehaviorCollection’

    Comment by Julian Ustiyanovych | August 30, 2010 | Reply

  53. ups, it works, sorry for spam :)

    Comment by Julian Ustiyanovych | August 30, 2010 | Reply

  54. 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?

    Comment by Marlon Grech | August 30, 2010 | Reply

  55. Hi,

    I’m trying to get a doubleclick on a ListboxItem, but I get an exception here because strategy is null. What am I doing wrong?

    public void Execute()
    {
    strategy.Execute(CommandParameter);
    }

    Comment by Mathieu | September 23, 2010 | Reply

  56. Hi,
    I have looked at your TestingPasswordBox Solution but I am however searching for a means of briefly displaying the actual character that is entered (this would help user if typing via a mobile app etc). Do you have suggestions on it may be possible to achieve this with a WPF PasswordBox.

    Best wishes

    Wayne

    Comment by Wayne Russell | October 25, 2010 | Reply

  57. First, thanks Marlon for the excellent library! Regarding the example “Support for Collection of binding to commands” I was wondering how/can that attached read only collection be set in a style?

    I’ve tried a couple things and get either a parameter count mismatch in the designer or if I try to define the bindings in a collection I get a runtime error.

    Comment by Anthony Franzese | November 4, 2010 | Reply

  58. Hey Marlon,
    Thanks for sharing this library. Helped me a lot. Was just wondering if I can use this with the Infragistics controls as well.

    Comment by Vijay BK | December 2, 2010 | Reply

  59. Hi Marlon,

    Thanks for your nice approach, Many Regards from my side.

    I ran into a situation where I wanted to Execute a command only when Enter key is pressed and also the button binding this command should provide the visual effect like the button is Pressed.

    For this I added a DefaultCommand in your solution and Used VisualTreeHelper to find the button binding the command under execution then changed buttons style before command execution and riverted the original button style after command has been executed.
    it works fine and appears like button was clicked.

    The problem is that it happend on Every Key press.I am not able to find a way to detect if Enter key was pressed.

    The situation occcurs when I bind the CommandBehaviourCollection with Event name provided along with command. LIke:

    Comment by Khamani | December 9, 2010 | Reply

  60. Attached Behaviour Property does not work if set through Styles or Templates

    Comment by Sandy | March 17, 2011 | Reply

    • Hello,
      I have combo box inside datagrid datatemplate. Here the SelectionChanged event is not firing because in CommandBehaviorBinding strategy is always null.
      The comment from Sandy i think is the problem that thorugh templates or style Behavior property doesn’t work. Any solution or workaround for this?
      Regards
      Imran

      Comment by imran | August 1, 2011 | Reply

  61. Attached Command Behaviour does not work if set through Styles or Templates..

    strategy always comes as null..Try it.. :)

    Comment by Sandy | March 17, 2011 | Reply

    • hello

      strange, its working for me..??????

      Comment by mourad | June 8, 2011 | Reply

  62. Sandy is correct. That problem is what is keeping me from using the library.

    Comment by John H | April 4, 2011 | Reply

  63. The Action property doesn’t seem to be set in those situations.

    Comment by John H | April 4, 2011 | Reply

  64. I read about another solution to this problem: The ‘RelayCommand’ class. There are some examples using the class on the net.

    Comment by A. Rohr | May 1, 2011 | Reply

  65. Can anybody out there plz…….. help me this is not working for the TabControl SelectionChnaged changed event some exception Object reference set to null etc is thrown…

    Comment by Abdul Hayee | June 2, 2011 | Reply

  66. I got a problem with ResourceDictionaries, it won’t work with them. If i call the code in the xaml-file everything works fine, but if i move the style to a ResourceDictionary, it won’t work. Has anyone a soltuion to this??

    Comment by opiswahn666 | June 29, 2011 | Reply

  67. I have no problems using it with styles, like this:

    Note that the binding to the command is usually set to an element above de ListViewItem. It is difficult to debug these binding errors, because they’ll throw apparently unrelated exceptions. So check your code carefully if it is not working for you.

    Comment by Ntx | June 30, 2011 | Reply

  68. Oh great, the xaml is gone from my comment. Check this:
    http://stackoverflow.com/questions/1035023/firing-a-double-click-event-from-a-wpf-listview-item-using-mvvm

    Comment by Ntx | June 30, 2011 | Reply

  69. Pretty awesome!

    Comment by James | July 17, 2011 | Reply

  70. [...] with compile-time checking. I won’t stop on it in details as you could read about it here and here or [...]

    Pingback by WPF 4.5 supports MarkupExtension in the event properties « A place for spare thoughts | September 27, 2011 | Reply

  71. Nice! Works perfect.. thanks!

    Comment by smashpapst | January 5, 2012 | Reply

  72. Thank you very much for publishing this. Very elegant!

    Comment by Helge Klein | January 20, 2012 | Reply

  73. if try to catch the Loaded event, the VS2010 Designer crash.
    I modify your code as is :
    namespace AttachedCommandBehavior
    {
    ///
    /// Defines the command behavior binding
    ///
    public class CommandBehaviorBinding : IDisposable
    {

    ……….

    ///
    /// Executes the strategy
    ///
    public void Execute()
    {
    try
    {
    if (CommandParameter != null)
    {
    strategy.Execute(CommandParameter);
    }
    }
    catch { }
    }

    Comment by anthony_69 | January 25, 2012 | Reply

  74. sorry……leave the if statement….

    Comment by anthony_69 | January 25, 2012 | Reply

  75. [...] assign one event to a command, or I usually recommend to design one AttachedCommand, like this did: http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb . But just checking the MVVM Light Toolkit for Metro, it cannot provide the EventToCommand [...]

    Pingback by Attached Command for Windows 8 Metro Style in C# | Programmer Solution | January 28, 2012 | Reply

  76. [...] Edit and Solved: Apparently MouseBinding steals the event and won’t pass it through. I solved it using AttachedBehaviors that we already had in our solution. I think taken from this http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/ [...]

    Pingback by ListBoxItem stealing Mouse clicks from ListBox | Build Future Repository | April 13, 2012 | Reply

  77. Great thanks for this library!
    But in the second version I can’t use this solution programmly (in code-behind). I was able to write such a code earlier:

    var polly = new Polygon();

    polly.SetValue(CommandBehavior.EventProperty, “MouseLeftButtonUp”);
    polly.SetBinding(CommandBehavior.CommandProperty, “MouseLeftButtonUpCommand”);

    and now I can’t set binding directly to my FrameworkElement. I have to create BehaviorBinding, which is Freezable and doesn’t support SetBinding:

    var behavior = new BehaviorBinding();
    behavior.Event = “MouseLeftButtonUp”;
    behavior.Owner = polly;
    behavior.Command = ???

    so, my question is how can I use wpf native binding directly from code??

    Comment by Orlan | May 15, 2012 | Reply

  78. [...] am trying to use AttachedCommandBehavior V2 to translate ListBoxItem events such as double click into commands that are execute against the [...]

    Pingback by Apply multiple AttachedCommandBehaviors using a style | PHP Developer Resource | May 24, 2012 | Reply

  79. [...] prefer to use AttachedCommand Behaviors, which allow you to attach a Command to just about any UI [...]

    Pingback by Capture DoubleClick in DataGrid | PHP Developer Resource | May 29, 2012 | Reply

  80. [...] I use an AttachedCommand Behavior which allows me to bind Events to ViewModel Commands. For example, you could [...]

    Pingback by How to handle WPF event in MVVM for nested controls in a Window | PHP Developer Resource | May 29, 2012 | Reply

  81. Please consider creating a nuget package for this…

    Comment by Stephen | June 19, 2012 | Reply

  82. Hello
    I want use with “PreviewMouseMove” event. But i don’t know how get the parameter “MouseEventArgs” in VM function.

    Comment by didier SUC | June 22, 2012 | Reply

  83. Did someone get it work with Silverlight 5. Freezable & FreezableCollection is missing in Silverlight ;-(
    Btw. it works awesome with WPF.

    Comment by Carlo | September 11, 2012 | Reply

  84. Tremendous things right here. I am very happy to find out your article.
    Thank you and I was having a look forward to touch you.
    Will you please drop me a e-mail?

    Comment by http://myjeeves.ask.com/ | January 9, 2013 | Reply

  85. [...] The font is called “Birth Of a Hero.ttf” it’s in the project folder under one called Fonts. You can get AttachedCommandBehavior here [...]

    Pingback by Jot the WPF Sticky Note App | Jarloo | January 23, 2013 | Reply

  86. Awesome article! I have 1 question (so far), How can I set an input gesture on the SimpleCommand so that my view will execute the SimpleCommand when it detects that gesture. I have tried using the RoutedCommand as well, but it seems that the view doesn’t receive the gesture, so it doesn’t get passed to the command on the ViewModel.

    Comment by xtr102 | March 7, 2013 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 845 other followers

%d bloggers like this: