pass command from 3rd party control to view model

Jan 3, 2014 at 12:41 PM
I've rewritten my app to use caliburn and mvvm its taken ages and has been difficult. my final hurdle is to wire the a 3rd party control to call my refresh method in the viewmodel
the 3rd party control has an event handler in the view, when this is triggered I just want to call myviewmodel.refresh - how is this best accomplished
Jan 3, 2014 at 10:04 PM
Edited Jan 3, 2014 at 10:07 PM
You can treat the event as you would on the view in the view model...

Just wire up the event to the viewmodel through Message.Attach="[Event EventName] = [Action SomeAction($eventArgs)]" on the control in question. Assuming you know the signature of the Method which can be discovered through just creating the handler on the view first then recreating on the viewmodel by hand (copy/past) with said signature..
// For Example
public void SomeAction(EventArgs e){ 

if (e.SomeProperty == somethingtrue)



public void SomeAction(){

or use IEventAggregator on the view... and create an object that you want to send through to the viewmodel with contents of which say "refresh" or something... creating an object of IEventAggregator on the view is relatively easy it doesn't include Injection.

I am sure some would disagree on the later call..

in the end both methods are still in the realm of separation of concerns...
Jan 4, 2014 at 8:23 AM
thanks that's really great, I've managed to message.attach to refresh my data when the pulltorefresh action on the listbox is called.

finally I would like to call this after the refresh - which stops the animation on the telerik control....

(code behind)

how would I do this from the viewmodel

sorry for the very basic questions
Jan 4, 2014 at 6:18 PM
Edited Jan 4, 2014 at 6:20 PM
I too am a Telerik user and haven't gotten to the point to using pulltorefresh and therefore never looked how to wire up the completion, I suspect that the completion event comes only on the view side so you would probably have to handle on the view which is entirely ok. Ieventaggregator would be my first go to on this.. as I stated before snag instance of it and then listen for "somecompletioneventyoucreate" is published. I have some snippets laying around if need be for the instance of IeventAggregator in a view, it does smell but it works...

Another possibility is to pull an "instance" of the control into the viewmodel via the OnViewLoaded(object view) override and then using GetView() to get access to typed information then cast control to RadDataboundListBox since that is what it is then when you are doing do "controlname.StopPulToRefreshLoading(true);" call to stop animation.

Some are purists some are not, if it's entirely view related then do it on the view... but if it entails something else in the viewmodel then it gets slightly more complicated, just have to see about marshalling around the events you want to occur.

due to the nature of the beast that is WP and trying to force a paradigm that can be anti-pattern in some cases, this would be one of them.. Testability can be troublesome in this case due to the code involved, especially if the control name ever changed.

there aren't ever very basic questions when it comes to this stuff :)
Jan 4, 2014 at 8:16 PM
I'm not a purist - just interested in learning something new. I've just created a new instance and used GetView() to stop the animation. Works well and I don't need to introduce EventAggregators (if I do this I will spend the next 2 weeks thinking about these)....Looks like I could be finally ready to publish v.2 of my app - a couple of weeks late but it looks good and should be much easier to maintain than all my previous code.... many thanks!