I hope a simple, albeit contrived question about the MVVM.
My MainWindow has 1 control – the ContentControl which displays one of only 2 views. I want to be able to switch between the 2 views via a button. The problem is, the buttons will be a control on each View. EG View1 has a button and View2 has a button. Only 1 view is every shown on the UI and by clicking the button the other view will be displayed. However, for this to work it will mean the ViewModel of the View will have to know about the MainWindow’s ViewModel in order to change the View. This seems wrong.
The issue may be my MainWindow ViewModel. One of the projects is public object View {get;set} and this is what binds to the MainWindows ContentControl. So, it’s this property which needs to be updated from the View’s viewmodel.
Does it go against the MVVM pattern if the View updates the MainWindow’s ViewModel?
I am not sure if I totally understand your question but a good technique to communicate between viewmodels is either via the EventAggregator oder Messenger Pattern.
These two implement pub/sub in a loosly coupled way.
This is an example of MVVMLight Toolkit’s Messenger
http://dotnet.dzone.com/articles/mvvm-light-whats-messenger
This is an example of Prism’s EventAggregator to communictate between viemodels
http://rachel53461.wordpress.com/2011/06/05/communication-between-viewmodels-with-mvvm/
hope this helps..
Edit: Ok, my answer is still valid. If you you use pub/sub like mentioned above you would tell
view1andview2to send out a change view message for example and define a target (target could beview2for example). Then you would subscribe in yourMainViewModelto every change view message. And when a change view message arrives. TheMainViewModelgets notified and executes an event and does not necessarily have to know who sent the message.And remember mvvm is only a pattern not a religion … just use the things that keeps you going and have the pattern in the back of your head =)…