5

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!

Community
  • 1
  • 1
Stunna
  • 413
  • 6
  • 16
  • 2
    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 Jul 29 '14 at 17:09
  • 1
    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 MVVM](http://rachel53461.wordpress.com/2011/12/18/navigation-with-mvvm-2/) – Rachel Jul 29 '14 at 17:31
  • 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 Jul 29 '14 at 17:32
  • I tried following your example as you have a variety of information on your blog Rachel which are really great examples, but I couldn't tease out that dockpanel.left button and put that in my view model, and then get the viewmodel to invoke a datacontext from the sub viewmodel, if that makes sense? – Stunna Jul 29 '14 at 17:35
  • 2
    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](http://rachel53461.wordpress.com/2011/06/05/communication-between-viewmodels-with-mvvm/) to pass broadcast a message, and have your child VM subscribe to those messages. – Rachel Jul 29 '14 at 17:40
  • I was hoping to have the entire view change. I was thinking of using a viewmodellocator and using light messenger to pass messages back to the viewmodellocator to then trigger the currentmodelview to change to another viewmodel. But this is all easy to talk about, difficult to implement. – Stunna Jul 29 '14 at 18:36
  • @Stunna Personally I would get rid of the ViewModelLocator altogether, but that's just me. :) You would still do the same thing with a `ContentControl`, except your ParentViewModel would default to showing the View you have now, and when the button is clicked it should broadcast a ChangeView message. Your ParentViewModel would subscribe to the `ChangeView` message type, and whenever one occurs it would change `SelectedViewModel` to whatever view should be displayed. Using MVVM Light, the calls you'll want are `Messenger.Default.Register` to subscribe and `Messenger.Default.Send` to broadcast – Rachel Jul 29 '14 at 19:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58283/discussion-between-stunna-and-rachel). – Stunna Jul 29 '14 at 19:09
  • 1
    @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 Oct 07 '14 at 01:32

1 Answers1

2

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!

Community
  • 1
  • 1
Stunna
  • 413
  • 6
  • 16