title: How should the ViewModel close the form?
body: <p>I'm trying to learn WPF and the MVVM problem, but have hit a snag.
  This question is similar but not quite the same <a href="https://stackoverflow.com/questions/454868/handling-dialogs-in-wpf-with-mvvm">as this one (handling-dialogs-in-wpf-with-mvvm)</a>...</p>
  <p>I have a &quot;Login&quot; form written using the MVVM pattern.</p>
  <p>This form has a ViewModel which holds the Username and Password, which are bound to the view in the XAML using normal data bindings.
  It also has a &quot;Login&quot; command which is bound to the &quot;Login&quot; button on the form, agan using normal databinding.</p>
  <p>When the &quot;Login&quot; command fires, it invokes a function in the ViewModel which goes off and sends data over the network to log in. When this function completes, there are 2 actions:</p>
  <ol>
  <li><p>The login was invalid - we just show a MessageBox and all is fine</p>
  </li>
  <li><p>The login was valid, we need to close the Login form and have it return true as its <code>DialogResult</code>...</p>
  </li>
  </ol>
  <p>The problem is, the ViewModel knows nothing about the actual view, so how can it close the view and tell it to return a particular DialogResult?? I could stick some code in the CodeBehind, and/or pass the View through to the ViewModel, but that seems like it would defeat the whole point of MVVM entirely...</p>
  <hr />
  <h3>Update</h3>
  <p>In the end I just violated the &quot;purity&quot; of the MVVM pattern and had the View publish a <code>Closed</code> event, and expose a <code>Close</code> method. The ViewModel would then just call <code>view.Close</code>. The view is only known via an interface and wired up via an IOC container, so no testability or maintainability is lost.</p>
  <p>It seems rather silly that the accepted answer is at -5 votes! While I'm well aware of the good feelings that one gets by solving a problem while being &quot;pure&quot;, Surely I'm not the only one that thinks that 200 lines of events, commands and behaviors just to avoid a one line method in the name of &quot;patterns&quot; and &quot;purity&quot; is a bit ridiculous....</p>