One thing I struggle with on MVVM pattern is when I try to implement additional Windows. When I create an additional Window and ViewModel to go along with that Window that is going to serve a particular purpose my design seems to get lost.
Take for example I have a very simple application. It’s a list of people from which I can Add/Delete/Restore. My initial design I handle Delete/Restore from the ViewModel, but Adding I must request information from the user. Given this, I created a new Window and ViewModel (AddWindow/AddViewModel). At this point I get lost. I’ve read about creating Controllers to communicate between, but I’m not even sure that makes sense.
On the AddWindow a user can fill in two fields (first name/last name) and then click Add (or cancel) in which case at that point the data needs to be passed back to the ViewModel so it can be added to the collection so it’s updated (also the AddWindow must close).
How can I think about this in a better manner and what can I do here to resolve such a simple problem?
Create a new class, ApplicationModel, to hold all the user data. Your “Main” view model should create it and then hold it in a property. Your View need to bind to the full path to the data in the model:
In the constructor of your new ViewModels you supply that Model as a parameter. That way all your ViewModels will share the same Model and the new ViewModels can modify/view the same data as your “main” ViewModel.