1
votes

I am trying to develop a WPF program using MVVM design which has two controls: browser and textbox.

I want to have two ViewModels, one having the logic for the browser and another for the textbox. I've looked around everywhere but RelayCommand seems to be only used to bind commands of Views to ViewModels whereas in my code, I also need the two ViewModels to communicate with each other. For example, the mechanism I'm thinking is where the when the browser loads a web page, the browser ViewModel extracts the HTML code, and then does some processing/formatting on the code and once its done, it will assign the code to a property in the browser ViewModel and raise an event. Once this is done, the textbox ViewModel will listen to the event from the browser ViewModel (or any other ViewModels) and assign the text as the content of the textbox.

Is there anyway to achieve this or is the only way to have a parent ViewModel which contains the two child ViewModels?

Thanks

2
I urge you to use Caliburn micro and not Prism . Prism is allot to take in. with caliburn you some powerful features which require some learning but the documentation isn't that long.eran otzap

2 Answers

2
votes

I would use loosely coupled pub/syb events using Prism.PubSubEvents(nuget package) https://www.nuget.org/packages/Prism.PubSubEvents/

Communicating between loosely coupled components in a Windows Store business app using C#, XAML, and Prism https://msdn.microsoft.com/en-us/library/windows/apps/xx130639.aspx

I hope this helps.

2
votes

Yeah, it is possible and simple. Just use the event aggregator pattern to communicate between view models. The best article I've ever read is by Magnus Montin.

What EventAggregator pattern is here.