title: WPF MVVM - Simple login to an application
body: <p>I'm continuing to learn WPF, and focusing on MVVM at the moment and using Karl Shifflett’s "MVVM In a Box" tutorial. But have a question about sharing data between views/viewmodels and how it updates the view on the screen. p.s. I haven't covered IOC's yet.</p>
  
  <p>Below is a screenshot of my MainWindow in a test application. Its split into 3 sections (views), a header, a slide panel with buttons, and the remainder as the main view of the application. The purpose of the application is simple, login to the application. On a successful login, the login view should disappear by it being replaced by a new view (i.e.  OverviewScreenView), and relevant buttons on the slide of the application should become visible.</p>
  
  <p><img src="https://i.stack.imgur.com/COxLd.png" alt="Main Window"></p>
  
  <p>I see the application as having 2 ViewModels. One for the MainWindowView and one for the LoginView, given the MainWindow doesn't need to have commands for Login so i kept it separate.</p>
  
  <p>As i haven't covered IOC's yet, I created a LoginModel class which is a singleton. It only contains one property which is "public bool LoggedIn", and an event called UserLoggedIn.</p>
  
  <p>The MainWindowViewModel constructor registers to the event UserLoggedIn. Now in the LoginView , when a user clicks Login on the LoginView, it raises a command on the LoginViewModel, which in turn if a username and password is correctly entered will call the LoginModel and set LoggedIn to true. This causes the UserLoggedIn event to fire, which is handled in the MainWindowViewModel to cause the view to hide the LoginView and replace it with a different view i.e. an overview screen.</p>
  
  <p><strong>Questions</strong></p>
  
  <p>Q1. Obvious question, is logging in like this a correct use of MVVM. i.e. Flow of control is as follows. LoginView --> LoginViewViewModel --> LoginModel --> MainWindowViewModel --> MainWindowView.</p>
  
  <p>Q2. Assuming the user has logged in, and the MainWindowViewModel has handled the event. How would you go about creating a new View and putting it where the LoginView was, equally how do you go about disposing of the LoginView once it is not needed. Would there be a property in the MainWindowViewModel like "UserControl currentControl", which gets set to LoginView or a OverviewScreenView.</p>
  
  <p>Q3. Should the MainWindow have a LoginView set in the visual studio designer. Or should it be left blank, and programatically it realises that no one is logged in, so once the MainWindow is loaded, then it creates a LoginView and shows it on the screen. </p>
  
  <p>Some code samples below if it helps with answering questions</p>
  
  <p><strong>XAML for the MainWindow</strong></p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
      xmlns:local="clr-namespace:WpfApplication1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="372" Width="525"&gt;
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="Auto" /&gt;
              &lt;ColumnDefinition Width="*" /&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition Height="*" /&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;local:HeaderView Grid.ColumnSpan="2" /&gt;
  
          &lt;local:ButtonsView Grid.Row="1" Margin="6,6,3,6" /&gt;
  
          &lt;local:LoginView Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>MainWindowViewModel</strong></p>
  
  <pre><code>using System;
  using System.Windows.Controls;
  using WpfApplication1.Infrastructure;
  
  namespace WpfApplication1
  {
      public class MainWindowViewModel : ObservableObject
      {
          LoginModel _loginModel = LoginModel.GetInstance();
          private UserControl _currentControl;
  
          public MainWindowViewModel()
          {
              _loginModel.UserLoggedIn += _loginModel_UserLoggedIn;
              _loginModel.UserLoggedOut += _loginModel_UserLoggedOut;
          }
  
          void _loginModel_UserLoggedOut(object sender, EventArgs e)
          {
              throw new NotImplementedException();
          }
  
          void _loginModel_UserLoggedIn(object sender, EventArgs e)
          {
              throw new NotImplementedException();
          }
      }
  }
  </code></pre>
  
  <p><strong>LoginViewViewModel</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows.Controls;
  using System.Windows.Input;
  using WpfApplication1.Infrastructure;
  
  namespace WpfApplication1
  {
      public class LoginViewViewModel : ObservableObject
      {
          #region Properties
          private string _username;
          public string Username
          {
              get { return _username; }
              set
              {
                  _username = value;
                  RaisePropertyChanged("Username");
              }
          }
          #endregion
  
          #region Commands
  
          public ICommand LoginCommand
          {
              get { return new RelayCommand&lt;PasswordBox&gt;(LoginExecute, pb =&gt; CanLoginExecute()); }
          }
  
          #endregion //Commands
  
          #region Command Methods
          Boolean CanLoginExecute()
          {
              return !string.IsNullOrEmpty(_username);
          }
  
          void LoginExecute(PasswordBox passwordBox)
          {
              string value = passwordBox.Password;
              if (!CanLoginExecute()) return;
  
              if (_username == "username" &amp;&amp; value == "password")
              {
                  LoginModel.GetInstance().LoggedIn = true;
              }
          }
          #endregion
      }
  }
  </code></pre>