views:

73

answers:

1

Specifically in MVVM Light toolkit? I've not dove into the Prism code yet to see what it does in regard to regions in a WPF UI. Seems like with the messaging and the ViewModelLocator in MVVM Light you could do a similar thing....Can you? Can anyone give some examples on how you could do this? Essentially I'd like to click on a button and load two different view models into the UI. Perhaps a better way to explain is Outlook-like Navigation Pane functionality.

+2  A: 

This can be done fairly easily in WPF, without any framework.

Just setup a DataTemplate in your Application (or at the Window/UserControl level) that maps the ViewModel to the View you wish to display for that ViewModel.

You can then just use a ContentPresenter, and bind it's contents to a single property (which can be of type object) within your ViewModel. When you want to set the "region" to a specific View, just set the property to the appropriate ViewModel, and WPF will automatically wire up everything for you.

Reed Copsey
Sweet, I figured there should be a XAML way too. Thanks.
nportelli
Ok. I must be not understanding enough. I suppose I didn't set up a DataTemplate. Not sure how to map the ViewModel to the View doing that. I'm assuming it is so I don't get the object name like I am now. But does that mean the view's have to know what viewmodel to display?
nportelli
@nportelli: The View layer, somewhere, needs to specify that "ViewModel XXX goes to View YYY". That's it. Read my section on "Templating" (and optionally look at the sample code) from my MVVM series. It uses this technique and describes how to make the DataTemplates work: http://reedcopsey.com/series/windows-forms-to-mvvm/
Reed Copsey
Ok after looking at this solution more I'm not sure I like it. It requires the main view or parent view to know about all the others...seems dirty. Am I just being too picky?
nportelli
@nportelli: It's picky, but it bothers me too. I actually use DI to inject the DataTemplates into my application resources, and use merged resource dictionaries (resolved at runtime) to handle this mapping. That keeps me from having to actually put it into the code at a "parent" or "main view" level...
Reed Copsey