Get Access to a control in the view from the viewmodel

Topics: Prism v4 - Silverlight 4
Sep 14, 2011 at 5:59 PM


I am using VS 2010, Prism V4, SL 4, and MVVM.

All that is working fine.

My Question is I have a control in the View, and I would like to pass that control back to the view model to do custom logic etc. 

How can I do that, a simple few lines of codes will be very helpful and appreciated.






Sep 14, 2011 at 7:10 PM


The common approach when following the MVVM pattern is that the ViewModel should not know about the View or any of its child controls. Instead, the ViewModel should be the DataContext of the View, which would retrieve the data and values for the controls' properties from the ViewModel's properties through Data Binding and request the ViewModel to process data through Commands.

If the logic that you need to apply to your control is only used to change the purely visual aspects of the information, you can put that logic in the View's code behind.

Therefore, passing the Control to the ViewModel isn't a recommended approach when following the MVVM pattern.

For more information about the MVVM pattern you can check the following links:

I hope you find this useful,

Damian Cherubini

Sep 14, 2011 at 7:14 PM

Thank you so much for your quick reply.  

I fully understand and I agree with what you said.

Unfortunately, the control we are using (third party control) does not really support data binding for most of the stuff I am using for our business need.

Nonetheless, I would like to pass the control to the viewmodel.  What is the best way to do that.





Sep 14, 2011 at 9:42 PM


In this case, to “pass” the Control from the View to the ViewModel, you could implement a Shared Service, which could be referenced by both the View and the ViewModel, and store a reference to the Control in that service. For example, the Shared Service could be injected in the View’s constructor, where it could store the Control in the service without any other code behind, and then the ViewModel could obtain the Control through the service and apply the required logic when needed. Doing this, the View and the ViewModel are still “decoupled,” but the ViewModel has to know how to handle the control.

Also, another possible and quick approach for this scenario could be to apply the required changes to the Control in the View’s code behind. For this, you could obtain any required data from the ViewModel through bindings with other controls or casting the View’s Data Context (which should be the ViewModel) to the corresponding interface and then access the required data directly from the ViewModel’s Properties. With this approach the ViewModel doesn’t need to know about the Control, but the View interacts with the ViewModel without using bindings, which is not a recommended practice.

Please, take into account that both of these approaches don’t comply with the recommended practices given when following the MVVM pattern.

I hope you find this useful,

Damian Cherubini