I have an application like the one below (i know the colors are awful, I just made it this way to make it easier to explain):
It has 3 views and 3 viewmodels. A mainview(the gray one) which contains a content control which will change the view depending on which button is clicked, the homeview(the green one) which lets the user pick a file, and the blue one which is supposed to read out the contets of the file
MainView with FileView
The button for selecting the file is implemented in the codebehind of the homeview, it's just a simple openfiledialog, what I need is to pass the result of the openfiledialog from the homeview's (green one) code behind to the file's view (the blue one) or maybe to the FileViewModel (not sure which one is more appropriate here) so that it can show the contents of the file in the blue view. What I was trying to do was to call a command to try to communicate between these two views and pass the file path between them but i'm not sure if this is the appropriate approach.
CodePudding user response:
You could use an event aggregator or a messenger to send an "event" or "message" from one component in your application to another one in a loosely coupled way.
Another similar option is to inject your view models (or other components) with a shared service and communicate between them using this one.
The third option is to use direct references, i.e. your HomeViewModel
has a reference to the FileViewModel
or vice versa. Note that this creates a tight coupling between your types which is usually something that you want to avoid for maintainability reasons.
You may also consider binding two views to the same view model.