Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Typically you would enable intra-view communication using the <a href="http://www.dofactory.com/Patterns/PatternMediator.aspx" rel="nofollow">mediator pattern</a>, and not via a dependency property. This allows you to develop the views and controls independently of one another, as a mediator will provide loose coupling between the views.</p> <p>Assume you have 2 views, with View A containing Control A and View B containing Control B. You also have a view model for each view. The view model is where communication between views is going to occur by using a shared mediator. Alternately, you could add the mediator to each control as a dependency property and bind that property to the view model's mediator.</p> <p>View A</p> <ul> <li>Contains Control A</li> <li>Has its DataContext set to ViewModel A</li> <li>User interaction with Control A gets/sets view model properties using data binding</li> <li>View model contains a shared reference to the mediator/messenger</li> </ul> <p>View B</p> <ul> <li>Contains Control B</li> <li>Has its DataContext set to ViewModel B</li> <li>User interaction with Control B gets/sets view model properties using data binding</li> <li>View model contains a shared reference to the mediator/messenger</li> </ul> <p>So when a user interacts with control A on view A, the interaction causes the mediator to publish a message that contains information about the event. The view model of view B is subscribed to be notified when this message is published, and updates its properties, which triggers a change in control B via data binding.</p> <p>The mediator used by both view models is the same instance, often injected into the view models using an IoC container. Implementing a mediator is fairly trivial, and there are also several MVVM toolkits like MVVM Light that provide a 'Messenger' class that enables this sort of communication.</p> <p>The key idea is that your views and controls no longer have any knowledge of each other, and instead interaction is abstracted as the publication and subscription of messages; facilitating loosely coupled communication between different objects and object types.</p> <p>Mediator and MVVM resources:</p> <ol> <li><a href="http://marlongrech.wordpress.com/2009/04/16/mediator-v2-for-mvvm-wpf-and-silverlight-applications/" rel="nofollow">Simple Mediator</a></li> <li><a href="http://mvvmlight.codeplex.com/" rel="nofollow">MVVM Light Toolkit</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/gg405484%28v=PandP.40%29.aspx" rel="nofollow">Implementing the MVVM Pattern</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/ff921122%28v=PandP.40%29.aspx#sec8" rel="nofollow">Event Aggregation</a></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload