title: Close Window from ViewModel
body: <p>Im creating a Login using a <code>window control</code> to allow a user to login into a <code>WPF</code> application that I am creating. </p>
  
  <p>So far, I have created a method that checks whether the user has entered in the correct credentials for the <code>username</code> and <code>password</code> in a <code>textbox</code> on the login screen, <code>binding</code> two <code>properties</code>. </p>
  
  <p>I have achieved this by creating a <code>bool</code> method, like so;</p>
  
  <pre><code>public bool CheckLogin()
  {
      var user = context.Users.Where(i =&gt; i.Username == this.Username).SingleOrDefault();
  
      if (user == null)
      {
          MessageBox.Show("Unable to Login, incorrect credentials.");
          return false;
      }
      else if (this.Username == user.Username || this.Password.ToString() == user.Password)
      {
          MessageBox.Show("Welcome " + user.Username + ", you have successfully logged in.");
  
          return true;
      }
      else
      {
          MessageBox.Show("Unable to Login, incorrect credentials.");
          return false;
      }
  }
  
  public ICommand ShowLoginCommand
  {
      get
      {
          if (this.showLoginCommand == null)
          {
              this.showLoginCommand = new RelayCommand(this.LoginExecute, null);
          }
          return this.showLoginCommand;
      }
  }
  
  private void LoginExecute()
  {
      this.CheckLogin();
  } 
  </code></pre>
  
  <p>I also have a <code>command</code> that I <code>bind</code> to my button within the <code>xaml</code> like so;</p>
  
  <pre><code>&lt;Button Name="btnLogin" IsDefault="True" Content="Login" Command="{Binding ShowLoginCommand}" /&gt;
  </code></pre>
  
  <p>When I enter in the username and password it executes the appropriated code, whether it being right, or wrong. But how can I close this window from the ViewModel when both username and password are correct?</p>
  
  <p>I have previously tried using a <code>dialog modal</code> but it didn't quite work out. Furthermore, within my app.xaml, I have done something like the following, which loads the login page first, then once true, loads the actual application.</p>
  
  <pre><code>private void ApplicationStart(object sender, StartupEventArgs e)
  {
      Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
  
      var dialog = new UserView();
  
      if (dialog.ShowDialog() == true)
      {
          var mainWindow = new MainWindow();
          Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
          Current.MainWindow = mainWindow;
          mainWindow.Show();
      }
      else
      {
          MessageBox.Show("Unable to load application.", "Error", MessageBoxButton.OK);
          Current.Shutdown(-1);
      }
  }
  </code></pre>
  
  <p><strong>Question: How can I close the Login <code>Window control</code> from the ViewModel?</strong></p>
  
  <p>Thanks in advance.</p>