Question

Hi I would like to grow my application by having discrete units of functionality in UserControls, and then put several of these inside a grid of ContentControls on my main page. I will need to be able to communicate between the controls in the grid.

I am wondering, what is the best way for them to exchange data. The application is an MVVM design, with each Usercontrol having its own ViewModel. Each of the main form's ContentControls 'Content' property is bound to a property in the main form's viewmodel which provides the Usercontrol to be displayed in that ContentControl.

For example, lets say I want an enhanced file browser, so I build it into a UserControl. Then I have a file viewer in another UserControl. I would like to be able to show them both at the same time in a grid on the main form. When I select a file with the browser, I want the selected file to be available to the Viewer Usercontrol.

I used this design so that I could reuse each chunk of functionality wherever I needed it in the application. I am not clear on how best to transfer data between them, as in my design they are not aware of each other. Any advice? Thanks!

Was it helpful?

Solution

Use a message bus. If you're using the MVVM Light Tookit, for example, use the in-built IMessenger implementation. If not, it's reasonably easy to roll your own.

Once you have a message bus, you can register for messages in one ViewModel and publish messages from another. It's an easy way to communicate (either one to one or in a "broadcast" fashion) across ViewModels.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top