5
votes

My attempts at trying to stick with the MVVM design pattern has left me spinning in my tracks. I have a view that is a pick list of site locations. I have a viewmodel that the view gets its data context from and I've bound some items to it with buttons and such (yay!).

Now I'm trying to switch the current view from a button click from within the view. My page

By clicking on the "start maintenance" button I would like to switch the view to another view with a different viewmodel.

So I know MVVM is just a design pattern and there seems to be a bajillion different ways of implementing navigation with MVVM. But the majority of these solutions I've seen point to having a main navigation pane and that is not what i am intending to do.

I plan on now trying to use a viewmodellocator with MVVM light messenger pattern to try and get my views to change. But after spending the past 3 days trying to shoehorn this thing to work, I'm growing desperate. Are there any other suggestions on how to implement this? I do like Sheridan's answer to a similar post:WPF MVVM navigate views because it avoids using a toolkit/framework. But I think the answer was perhaps too vague for me and I couldn't implement it because I didn't understand how to change the views (Sheridan's custom relay message was hard to follow as a novice).

Help please you Internet Denizens! :) If you could point me to any examples that would appreciated as well!

1
I use Sheridan's approach all the time. If you elaborate on what questions you have about it, I may be able to help (i.e., is it the XAML or the ICommand property that is hard to follow WRT the custom relay message?).Keith Kurak
The most basic way is to use a ContentControl and bind it's Content to a SelectedViewModel, and then use implicit DataTemplates to tell WPF how to draw each ViewModel. I have a basic example with no 3rd party libraries on my blog here if you're interested : Navigation with MVVMRachel
In Sheridan's example of his ICommand in the MainViewModel, his action command takes two parameters but I can only stuff 1 in it?Stunna
When you click on "Start Maintenance", are you expecting just the grid to be replaced, or the entire window to be replaced? If it's just the Grid, that's easy. You can place a ContentControl there, and the default .Content property would be your Grid. When you click on the button, the .Content would change to some other View. If you want the whole window to change, you may want to look into using a messaging system to pass broadcast a message, and have your child VM subscribe to those messages.Rachel
@Rachel-- The end of your conversation was moved into chat, which is now gone. Can you please post the solution as an answer? Thanks!Enzo Mac

1 Answers

2
votes

A big thank you to Rachel! I created a mainviewmodel that instantiates the other view models in my app. Then I used MVVM light to send a message using a template of a generic object to stuff the message between the models which then allowed me to handle the message in my main view model and switch the viewmodel context!

Thanks for all the help!