Re-using Views in view first MVVMC

Topics: Prism v4 - WPF 4
Apr 5, 2013 at 12:57 PM
Hi, I've inherited an application which uses the view-first MVVMC patern

In the application I've created 2 step process which creates a person and assigns them to a group. To do this I've created a view and corresponding view model (all views have a 1-2-1 relationship with a view model, view models are injected into the View constructor and are registered with the container using the TransientLifetimeManager) called CreatePersonMaster, the view simply contains a region (shown by the dashed line) which sub-views can be loaded into and the view model subscribes to two loosely coupled events, "PersonCreated" and "GroupSelected". The "PersonCreated" event saves a Person entity in a field and the "GroupSelected" event takes the saved Person, creates a Group association and saves them to a database.

Obviously this view/view model doesn't do anything until the events get raised so I load the following sub views into my the region.


These views/view models fire the events which get handled by the master view.

I also have an edit view where I want to re-use the select group view.


I can do this by subscribing to the appropriate events in the EditPersonMaster view model.

My question really is, is this the appropriate way to do this?

Because I'm using events I don't get any feedback into the View/ViewModels if there's an error when creating/reassigning? I could probably fire another "ErrorBlah" event for the inner view/model to handle and update the view.
Apr 5, 2013 at 7:20 PM

Based on my understanding, the "proper" architecture to be used in an application depends of the requirements, design decision and preferences of each project and team. In this case, I believe the architecture you are describing is a valid approach to implement the requirements of your scenario. However, there are also other approaches you might find interesting too.

For example, you could avoid using events by sharing the view models between the "master views" and its inner views: if the CreatePersonMasterView has its view model set in the DataContext property, the CreatePersonView and AssingToGroupView could "inherit" the DataContext resulting in the three views using the same CreatePersonMaster view model. Then, you would only need to define the corresponding properties for each view. Take into account that this might not work if you have a DataTemplate in between the master and children view.

Also, in order to easily reuse the AssignToGroupView you could define two regions instead of one in the master views, then inject the people view in one region and the group view in the other.

Damian Cherubini
Apr 8, 2013 at 10:16 AM
Thanks Damian

I am setting the DataContext and I forgot that, of course, it is cascaded down the UI control stack. Doh

I've stopped setting the DataContext on the sub-views and inject the ViewModels into the Master view models. That way I can perform any context based action in the master and also set any feedback properties on the sub view ViewModels.

Thanks for your help. I knew there must have been something I was missing.

Apr 8, 2013 at 2:43 PM
I've just realised what you mean about adding another region, I tried to inject AssignToGroups into my second master view and it bombed because the region names are different.

One question on this then, I don't want to display the content side by side, I only drew it like that above to illustrate they're in the same region. Do you recommend binding the visibility of a region to only show one at a time or disabling the views within a region so that only one control is shown (if I don't remove/hide then one lot of controls will sit on top of another)?

Apr 8, 2013 at 5:02 PM
It also seems like I should have had a look at the Navigation API including the NavigationJournal
Apr 8, 2013 at 6:30 PM
Hi Ben,

I'm glad I could be of help.

Regarding the regions, I thought that you wanted to two show both views at the same time, hence my recommendation of using two regions. If you only need to show one of the two views at a time and navigate back and forth between them, you will only need one region where you could switch the views as needed. As you mentioned, Prism's navigation API can be useful to implement this, as it allows you to navigate back and forth between views, pass parameters as strings (for example the Person id,) etc.

You can find more information about the Prism's navigation API in the following section of the documentation:

Damian Cherubini
Apr 8, 2013 at 9:15 PM
I'd just read that chapter, thanks for the link though.

Due to not directly setting the DataContext on the child UserControls, their ViewModels don't participate in the INavigationAware pattern so still need to manually implement "back" functionality.

What a tangled web I'm getting myself caught up in with this application.