1

I am developing a view-based application using the MVVM-Light framework (C#). Essentially, I have a single window that opens with a content controller, and the view models determine which view is in the content controller.

I would like to run commands from each individual view model that changes the view. For instance, if the user were to press "Submit", the application would take them back to the main view.

The only thing I can think of is to have each view model require a ParentWindow argument of type Window in order to link back to the CurrentViewModel variable in my MainWindowViewModel. Is there a better way to do this?

jaysoncopes
  • 805
  • 3
  • 13
  • 26
  • 1
    `CurrentViewModel` could have an event that the main window listens to. Would be easy if all the `CurrentViewModel` types inherited from a base already – Jonesopolis May 16 '16 at 21:40
  • I think the better way is to not even think of putting any reference of a view inside any of your ViewModel. You can achieve this by means of utilizing the DataTemplate with the list of your ViewModels. And then you probably need a SelectedItem property for binding and/or shifting from one view to another. – tgpdyk May 17 '16 at 02:44
  • @Jonesopolis what do you mean by that? Could you expand a little? – jaysoncopes May 19 '16 at 15:26
  • @tgpdyk I need to update the current view model that is linked to the `ContentControl` so that I can have custom menus in each view, especially the `MainViewModel` (versus the `MainWindowViewModel`) – jaysoncopes May 19 '16 at 15:28

1 Answers1

1

In MVVM Light there is a feature called Messenger. You could use this to communicate between ViewModels.

Here is an answer with an example of that: MVVM Light Messenger - Sending and Registering Objects

Community
  • 1
  • 1
Dima
  • 340
  • 3
  • 12