I have a WPF app that uses a business object called Visit, it has a lot of child objects like patient, exam, and others. There are views and view models for editing these various child objects, so there is a view and view model for editing patient info and one set for exam info, etc. There is also a main window view model.
When I need to open a new Visit, I have a search screen that also has it’s own view model. It needs to open new visit from the database and notify all the other views that the visit has changed.
I’ve looked into WeakEventManager, and also having one view model that is the parent of all the others, but I’m not sure what is the best way to proceed. I’d like to know what the relationship between the view models should be and how the open/search view model should tell all the other views to update. I have been calling OnPropertyChanged(“propname”) in my view models when a property is updated, but since the other views don’t know about the open/search view model, they don’t care if I say OnPropertyChanged(“Visit”)
Have a look at this post on SO that talks about the
Messenger. In your case you case post the Visit object and have the ViewModel capture that for display.If you have a very data centric views where the
Modeldata is pretty much presented directly on theViewwithout a lot of modification you can easily expose theModelas a property on theViewModeland have theViewbind to its properties.This way when one
Viewupdates theModelthe otherView‘s will update automatically without having to listen for property change events on theModelEdit:
To elaborate on my second point: You may or may not need this but if your
Modelalso implementsINotifyPropertyChangedthen any changes to that model will be propagated to theViewautomatically.If you need to have 2 Views with the Visit object then you can have the Visit property directly bound in XAML
Like I said this is only applicable if you need the same
Visitobject shared amounst 2 or moreViewModels and if theView‘s mostly are data centric or in other words the data from theModelis being presented directly on the screen. This is to avoid duplicating the properties in theViewModels and having to raise the property change event all the time.