title: MVVM Switching Between Views
body: <p>I'm new to WPF so bear with me. I have a WinForms Application that I am trying to redo in WPF. In my current WinForms Application, I stick all my Controls into one Form, and hide/show them based on what buttons are hit, as well as making use of a second form.</p>
  
  <p>My goal: Create different views to switch between smoothly based on what button is hit, instead of hiding Controls or making separate Forms and then hiding those.</p>
  
  <p>I currently have a MainWindow view (My initial launch window), where with a button, I switch to my CreateAccount view. What I am having issues with is, how can I make my button in my CreateAccount go "back" to my MainWindow?</p>
  
  <p>My end goal is to be able to switch between 4 views based off Button clicks. </p>
  
  <p>Here is my MainWindow.xaml</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Window x:Class="MusicPlayer.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:MusicPlayer"
          xmlns:Views="clr-namespace:MusicPlayer.Views"
          xmlns:ViewModels="clr-namespace:MusicPlayer.ViewModels"
          Title="MainWindow" Height="450" Width="800"&gt;
      &lt;Window.Resources&gt;
          &lt;DataTemplate x:Name="CreateAccountTemplate" DataType="{x:Type ViewModels:CreateAccountViewModel}"&gt;
              &lt;Views:CreateAccountView DataContext="{Binding}"/&gt;
          &lt;/DataTemplate&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid&gt;
          &lt;Button x:Name="TestButton" Content="Button" HorizontalAlignment="Left" Margin="164,182,0,0" VerticalAlignment="Top" Height="61" Width="68" Click="CreateAccountView_Clicked"/&gt;
          &lt;PasswordBox HorizontalAlignment="Left" Margin="164,284,0,0" VerticalAlignment="Top" Width="120"/&gt;
          &lt;ContentPresenter Content="{Binding}"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>My MainWindow.xaml.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Windows;
  using MusicPlayer.ViewModels;
  
  namespace MusicPlayer {
      public partial class MainWindow : Window {
          public MainWindow() {
              InitializeComponent();
          }
  
          protected override void OnClosed(EventArgs e) {
              base.OnClosed(e);
  
              Application.Current.Shutdown();
          } //end of onClosed
  
          private void CreateAccountView_Clicked(object sender, RoutedEventArgs e) {
              DataContext = new CreateAccountViewModel();
          } //end of CreateAccountView_Clicked
      }
  }
  </code></pre>
  
  <p>And here is my CreateAccount.xaml</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;UserControl x:Class="MusicPlayer.Views.CreateAccountView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:Views="clr-namespace:MusicPlayer.Views"
               xmlns:ViewModels="clr-namespace:MusicPlayer.ViewModels"
               mc:Ignorable="d" 
               d:DesignHeight="450" d:DesignWidth="800"&gt;
      &lt;UserControl.Resources&gt;
      &lt;/UserControl.Resources&gt;
      &lt;Grid Background="White"&gt;
          &lt;Button Content="Button" HorizontalAlignment="Left" Margin="276,279,0,0" VerticalAlignment="Top" Height="60" Width="59" Click="Button_Click"/&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>And my CreateAccountView.xaml.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System.Windows;
  using System.Windows.Controls;
  using MusicPlayer.ViewModels;
  
  namespace MusicPlayer.Views {
      public partial class CreateAccountView : UserControl {
          //public static readonly DependencyProperty TestMeDependency = DependencyProperty.Register("MyProperty", typeof(string), typeof(CreateAccountView));
  
          public CreateAccountView() {
              InitializeComponent();
          }
  
          private void Button_Click(object sender, RoutedEventArgs e) {
              DataContext = new MainWindowViewModel();
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/5ZINS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5ZINS.png" alt="Current Structure of my Project"></a></p>