title: WPF Switching Views through Command Button
body: <p>I'm quite new to WPF and what I'm trying to do is switch views through different command buttons.. this is my current code...</p>
  
  <p>Basically I have two buttons in my MainView, SetupLan and SetupSerialPort.
  For simplicity, I'm currently trying to make SetupSerialPort working first. But when I click the button for it, the command executes but the view doesn't change...</p>
  
  <p><strong>App.xaml</strong></p>
  
  <pre><code>&lt;Application.Resources&gt;
      &lt;DataTemplate DataType="{x:Type ViewModels:MainViewModel}"&gt;
          &lt;Views:MainView /&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate DataType="{x:Type ViewModels:SetupLanViewModel}"&gt;
          &lt;Views:SetupLanView /&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate DataType="{x:Type ViewModels:SetupSerialPortViewModel}"&gt;
          &lt;Views:SetupSerialPortView /&gt;
      &lt;/DataTemplate&gt;
  &lt;/Application.Resources&gt;
  </code></pre>
  
  <p><strong>MainWindow.xaml</strong></p>
  
  <pre><code>&lt;Grid&gt;
      &lt;ContentControl Content="{Binding}" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>MainWindow.xaml.cs</strong></p>
  
  <pre><code>public MainWindow()
  {
      InitializeComponent();
      this.DataContext = new MainViewModel();
  }
  </code></pre>
  
  <p><strong>MainViewModel</strong></p>
  
  <pre><code>public DisplaySetupSerialPortCommand SetupSerialPort
  {
      get; set;
  }
  
  public MainViewModel()
  {
      SetupSerialPort = new DisplaySetupSerialPortCommand(this);
  }
  </code></pre>
  
  <p><strong>MainView</strong></p>
  
  <pre><code>&lt;StackPanel&gt;
      &lt;Button Height="50" Width="150" HorizontalAlignment="Left"Command="{Binding SetupLan}"&gt;Lan&lt;/Button&gt;
      &lt;Button Height="50" Width="150" HorizontalAlignment="Left" Command="{Binding SetupSerialPort}"&gt;Serial Port&lt;/Button&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p><strong>DisplaySetupSerialPortCommand</strong></p>
  
  <pre><code>class DisplaySetupSerialPortCommand : ICommand
  {
      private BaseViewModel _baseViewModel;
  
      public event EventHandler CanExecuteChanged
      {
          add { CommandManager.RequerySuggested += value; }
          remove { CommandManager.RequerySuggested -= value; }
      }
  
      public DisplaySetupSerialPortCommand(BaseViewModel baseViewModel)
      {
          _baseViewModel = baseViewModel;
      }
  
      public bool CanExecute(object parameter)
      {
          return true;
      }
  
      public void Execute(object parameter)
      {
          _baseViewModel.ViewModel = new SetupSerialPortViewModel();
      }
  }
  </code></pre>