title: Open dialog in WPF MVVM
body: <p>I have an application that need to open a dialog from a button where the user enters some information.</p>
  
  <p>At the moment I do it like this (which works fine)</p>
  
  <ul>
  <li>The button click generates a command in the ViewModel.</li>
  <li>The ViewModel raises an event which the Controller listens to.</li>
  <li>The Controller works out the details of the new window (i.e. View, ViewModel &amp; model) and opens it (ShowDialog)</li>
  <li>When the window is closed the Controller adds the result to the eventargs and returns to the ViewModel</li>
  <li>The ViewModel passes the information to the Model.</li>
  </ul>
  
  <p>There are a lot of steps but they all make sense and there is not much typing.</p>
  
  <p>The code looks like this (the window asks for the user's name)</p>
  
  <p>ViewModel:</p>
  
  <pre class="lang-cs prettyprint-override"><code>AskUserNameCommand = DelegateCommand(AskUserNameExecute);
  ...
  
  public event EventHandler&lt;AskUserEventArgs&gt; AskUserName;
  
  void AskUserNameExecute(object arg) {
      var e = new AskUserNameEventArgs();
      AskUserName(this, e);
      mModel.SetUserName(e.UserName);
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre class="lang-cs prettyprint-override"><code>mViewModel.AskUserName += (sender,e) =&gt; {
      var view = container.Resolve&lt;IAskUserNameView&gt;();
      var model = container.Resolve&lt;IAskUserNameModel&gt;();
      var viewmodel = container.Resolve&lt;IAskUserNameViewModel&gt;(view, model);
      if (dlg.ShowDialog() ?? false)
          e.UserName = model.UserName;
  }
  </code></pre>
  
  <p>My question is how the horizontal communication works in the MVVM pattern.
  Somehow it seems wrong to let the controller be involved in the data transfer between the models.</p>
  
  <p>I have looked at the mediator pattern to let the models communicate directly. Don't like that idea since it makes the model depending on implemetations details of the GUI. (i.e. if the dialog is replaced with a textbox, the model need to change)</p>