Sharing ressources between ViewModels is a common scenario, however it is usually a good indicator of something going wrong in the architecture - or a lack thereof - if it does become a problem.
The easiest solution would be to have the UserControlVM
hold a reference to the MainWidowVM
and subscribe to a certain event which gets called by the interface via a Command
. Crossreferencing between ViewModels
which declare functionality can however become really really messy down the road with cross- and circle references eventually preventing GarbageCollection
from doing its job properly causing unwanted memory leaks.
You could also have your MainWindowVM
hold references to every other ViewModel
to provide a centralized point of communication by having crossreferences at least centralized.
The next easiest solution would be to implement something like an external messenging system with weak event binding. This gets rid of fixed references of objects within other objects but comes at the price of being extremely heavy on performance. If you are developing an app that has for instance a masterlist with elements to select and many modular detail-lists in different windows this might be feasible - oneway communication from 1 master-end with unfrequent changes and asynchronous implementation on many detail-ends. For your purpose this should be fine, if these events will be fired automatically however I suggest taking a different router entirely.
The problems listed above is why MVVM not only has Model
s, ViewModel
s and View
s but usually also use Services
to encapsulate shared logic and/or data (resources) off from ViewModels
into what's effectively a shared resource.
Your ViewModels (and maybe views in case of a LocalizationService
for instance) will hold references to a service and usually all to a single instance of that service. This also gets rid of unwanted cross-references and allows proper binding and proper (un-)subscription from events without the performance penalty of weak bindings.
The other upside to using Services
instead of crossreferencing ViewModels
is that it does give you the oppurtunity of actually following the SOLID principles of object-oriented programming and having the logic in the place it belongs without duplication.
Following that one could use an IoC-Container to properly inject references and dependencies at runtime, this however comes at the risk of hidden dependencies and a hell of a lot of preparation time with project setup, documentation, etc. if done improperly and/or for the first time. I personally wouldn't going this route unless every member of the team actually knows what they are doing entirely.