This project is read-only.

ViewModel share propblem

May 24, 2010 at 9:54 AM

Suppose that I have a WindowXXX which contain 3 UserControls, and they all use the same WindowXXXViewModel in their XAML file, 

How do I create multiple WindowXXX instances, and each WindowXXX has their own WindowXXXViewModel (but the UserControls still share the same WindowXXXViewModel) ?

May 24, 2010 at 9:24 PM

you can do it via an attribute

Put this attribute where you have your [ExportViewModel...


May 24, 2010 at 9:37 PM

If I use CreationPolicy.Shared, all the ViewModels will be the same, 

But I need different ViewModels in each WindowXXX instance.



May 24, 2010 at 9:54 PM

ow ok... sorry misunderstanding from my side...

so what you want is to share it in some places and to not share it in others... mmm... unfortunatly this is not supported right now in MEFedMVVM... In order to do this I would need to specify the CreationPolicy at import stage and right now I do not handle that case since AttachedProperties are not garanted to be set in the correct order in SL.

You can however workaround. This may be a bit of a hack but it will work quite nicely.

lets say your VM is Exported like this

public class WindowVMXX

and then define another VM like this

public class ChildVMXX : WindowViewModelXXXX

so the child one is not shared but the Window one is. Would that work for you?



May 24, 2010 at 10:35 PM

three problems:

1. All the UserControls in different Window will all have the same ChildVM, 

2. WindowVM and ChildVM are different in the same window.

3. the workaround won't work if I create many Windows.


maybe an option to allow CreationPolicy.Shared by

1.All (singleton)

2.Window (create container for each top window)

3.UserControl (create container for the specified UserControl)






May 24, 2010 at 10:46 PM

ok I appologize... I did not quite understand what you wanted to do... i thought you wanted to have a shared view model for all your windows and a seperate instance for the Child Usercontrols. Instead you want to have a Window that host 3 usercontrols and have those usercontrols share the same ViewModel.

So to accomplish this you can do the following...

so first you need a ViewModel

public class MyVM { }


now we have a window like this

<Window  ..... mef:ViewModelLocator.ViewModel="MyViewModel" >

<local:UserControl1 />




by doing so the same ViewModel will be injected to the usercontrols at runtime since DataContext is shared. Now you might say but hey I want to have Blend working here ! In order to do this in your case you will have to create another ViewModel that your usercontrol can use so that in Blend you get the ViewModel injected. BUT this ViewModel will be DataContextAware (a MEFedMVVM feature)

your new VM will be like this

[ExportViewModel("ChildVM", true)] public class ChildVM : MyVM { }

as you can see I am passing true in the ExportViewModel which tells MEFedMVVM that this ViewModel should not be created at runtime but only at design time.  to read more on this feature I suggest you read this post

Does this make sense?


May 24, 2010 at 10:47 PM

P.S you also need to specify the cild vm in your UserControl i.e

<UserControl ... mef:ViewModelLocator.ViewModel="ChildVM" />


May 25, 2010 at 10:25 AM
marlongrech wrote:


by doing so the same ViewModel will be injected to the usercontrols at runtime since DataContext is shared.

although I don't like it, but it works now.

Thanks for your help.



public class MyVM { }

May 25, 2010 at 10:38 AM

how would you want this to be... I am asking this because it might be something I consider for the next version

May 25, 2010 at 11:21 AM

I'm new in MVVM and MEF , I have no idea what it should be.