Passing parameter to ViewModel from View using Prism and MEF

Topics: Prism v4 - WPF 4
Mar 21, 2011 at 2:31 PM


This question has been haunting me a long time. How do we pass parameter from View to View model using Prism/MEF approaches?

I came up with a twiested way of doing it,

1. Created my View from my service class,

var x = ServiceLocator.Current.GetInstance<View>();

2. Get the handle of the Viewmodel property // I am using Property import of View Model class []

x.ViewModel.SomeProperty = 10; // Where SomeProperty is a interger type property defined in my ViewModel.

Is there a better approach? Where I can pass some parameter to View's constructor and from there I can hook it too ViewModel constructor?

Any pointers/links/snippets are highly appreciated.


Warm regards,


Mar 21, 2011 at 7:54 PM

Hi Indrasis,

While your approach fulfills your requirement of passing the value to the ViewModel, it isn’t a recommended practice, since it would make your application harder to test.

The recommended approach for passing values to the ViewModel is to use Two-Way Bindings. If you can’t do so, one workaround would be to use a service or controller class to share those values.

By doing that you could keep the testability of your application intact, as you could easily mock that service in case you’re testing.

You might find the following chapters useful:

I hope you find this information useful.


Miguel Bronzovic

Mar 22, 2011 at 4:43 AM

Hello Mig,

Thanks for the reply. I have looked into these chapters as mentioned, but was unable to find a solution to the problem stated.

Would it be possible for you to share some sample snippt?


Thanks in advance.

Warm regards,


Mar 22, 2011 at 4:50 PM

Hi Indrasis,

You might find the following sample useful, where the concepts explained by Miguel have been exemplified.

I hope you find this helpful.

Guido Leandro Maliandi

Mar 23, 2011 at 3:39 PM

Hi Guido,

This works perfectly.

Thanks a lot.