CustomEvents in MVVMLight RRS feed

  • Question

  • Is it possible to bind custom events to the command using "EventToCommand" (MVVMLight framework)? Has any one tried it? It is not working for me.

    I am currently evaluating the feasibility of using MVVMLight for our application and this is one think I am stuck up with. Also do you think MVVMlight will work better for CustomControls ?

    Any help is much appreciated..Thanks !

    Thursday, November 18, 2010 4:50 PM


All replies

  • Hi. I've used "EventToCommand" just some hours ago to bind a custom event, that should be no problem. Can you show us the code that is not working (the Xaml and your custom event)? 

    Also do you think MVVMlight will work better for CustomControls ?

    Better than what?

    Thursday, November 18, 2010 6:26 PM
  • Custom Event -

    public event EventHandler OnCancel;

    Raising the event -

     if(OnCancel != null) OnCancel(sender,e);


    <i:Interaction.Triggers> <i:EventTrigger EventName="OnCancel"> <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding Path=cmdCancel}" /> </i:EventTrigger> </i:Interaction.Triggers>
    Command  - 
    private RelayCommand<EventArgs> _cmdCancel =null; public RelayCommand<EventArgs> cmdCancel { get { if (_cmdCancel == null) { _cmdCancel= new RelayCommand<EventArgs>( e => OnActionCancel() ); } return __cmdCancel; } }


    Friday, November 19, 2010 9:21 AM
  • That's exactly how I do it too. Can you make sure your bindings (e.g. for the command) are really correct?

    Friday, November 19, 2010 9:41 AM
  • One more thing - the custom event is present inside a usercontrol (say UC1) dropped on the View (say V1).

    The command is present on the Viewmodel corresponding to V1.


    Friday, November 19, 2010 9:45 AM
  • I tried it again. But in vain Frown

    Friday, November 19, 2010 9:46 AM
  • Its working completely fine for normal events but not for custom events.

    Friday, November 19, 2010 9:50 AM
  • I have those in a custom control (not user control). Can you upload a small repro project that shows the issue?

    Friday, November 19, 2010 9:54 AM
  • Really strange. I tried reproducing this event in a test application. It works absolutely fine.But not in my application.

    I noticed one point by placing breakpoint on the commands in my application. On the intial load, the 'get' property of the command for this user control is never called while for other standard controls, it does get called. Any ideas ?

    This is one of the situations where you have no reasons why its not working in your application while it works everywhere else, even though the code is EXACTLY the same.

    Anyways, I am going to do a couple of more R&D on it. I will post the solution once I get it.

    Thanks for your help Smile

    Friday, November 19, 2010 12:33 PM
  • I am exposing all commands (also RelayCommands) as ICommand only, but that's about all the difference I can see from the information you provided.

    Saturday, November 20, 2010 4:50 AM
  • I debugged the SL4 source code for MVVMLight and I see the command as null for my case.

    I am using RelayCommands. Any insights ?

    Monday, November 22, 2010 3:58 PM
  • Instead of the lazy creation in the getter, did you try to create the command in the constructor, for example?

    Monday, November 22, 2010 5:55 PM
  • Tried that before Frown

    Monday, November 22, 2010 8:15 PM
  • Hi,

    Because MVVMLight is a third party component, there is bigger chance that you could get answer here.



    Best Regards,

    Friday, November 26, 2010 4:57 AM
  • I have run into the same problem.

    In my case, it was not working because my EventArgs were not inheriting from EventArgs. After I changed this, the EventToCommand started to work.

    It seems not to be an issue concerning to the EventToCommand behavior, but to the EventTrigger.

    Good Luck!

    • Edited by Andregsilv Tuesday, December 8, 2015 2:11 PM
    Tuesday, December 8, 2015 2:10 PM