title: How to display different views on MainWindow
body: <p>So I followed a video online that showed me how to implement MVVM in a WPF application. Please note that I am a newbie when it comes to both of them.</p>
  
  <p>I have the following classes that comprise the Model, View, and ViewModel layers of my WPF application:</p>
  
  <ul>
  <li>App class (.xaml and .xaml.cs)</li>
  <li>MainWindow class (.xaml and .xaml.cs)</li>
  <li>MainWindowViewModel.cs</li>
  <li>MalfunctionsView (.xaml and .cs)</li>
  <li>MalfunctionsViewModel.cs</li>
  <li>PartsView (.xaml and .cs)</li>
  <li>PartsViewModel.cs</li>
  </ul>
  
  <p>Basically I just have two views (Malfunctions and Parts) that get loaded up into the MainWindow view.</p>
  
  <p>I have it setup right now so that I can comment code in and out to show either the MalfunctionView or the PartsView in the MainWindow. For example, if I want to see the MalfunctionView then I comment out all of the PartsView code and then re-run it in VS. Yes, I know... it's sad and pathetic, but I haven't learned how to unload one view and load another view on the fly. Which brings me to my question: <strong>how do I unload one view from the MainWindow and then load up a different view into the MainWindow?</strong> For example, I have a button on the PartsView called Select, which when clicked then needs to unload the PartsView from the MainWindow and load up the MalfunctionsView in it's place.</p>
  
  <p>I am including the code that I have for the App class and MainWindow View and MainWindow ViewModel so that it can be seen how I am currently loading up ViewModels (user controls) into the MainWindow. </p>
  
  <p><em>App.xaml.cs</em></p>
  
  <pre><code>public partial class App : Application {
          protected override void OnStartup(StartupEventArgs e) {
              base.OnStartup(e);
  
              // Create MainWindow ViewModel
              var mainWindowVM = new MainWindowVM();
  
              // Create MainWindow View
              MainWindow mainWindowVw = new MainWindow();
  
              // Set MainWindow View datacontext to MainWindow ViewModel and then show the window
              mainWindowVw.DataContext = mainWindowVM;
              mainWindowVw.Show();   
          }
      }
  </code></pre>
  
  <p><em>MainWindow.xaml</em></p>
  
  <pre><code>&lt;Window x:Class="PAM.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:vm="clr-namespace:PAM.ViewModel"
          xmlns:vw="clr-namespace:PAM.View"
          Title="Parts and Malfunctions" Height="800" Width="550"&gt;
  
      &lt;Window.Resources&gt;
          &lt;DataTemplate DataType="{x:Type vm:MalfunctionsViewModel}"&gt;
              &lt;vw:MalfunctionsView /&gt;
          &lt;/DataTemplate&gt;
          &lt;DataTemplate DataType="{x:Type vm:PartsViewModel}"&gt;
              &lt;vw:PartsView /&gt;
          &lt;/DataTemplate&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Grid Width="Auto" Height="Auto"&gt;
          &lt;ScrollViewer&gt;
              &lt;ItemsControl Width="Auto" Height="Auto" ItemsSource="{Binding ViewModels}"&gt;&lt;/ItemsControl&gt;
          &lt;/ScrollViewer&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><em>MainWindowViewModel.cs</em></p>
  
  <pre><code>public class MainWindowViewModel : ViewModelBase {
  
          readonly MalfunctionRepository _mfRepo;
          //readonly PartsRepository _pRepo;
  
          ObservableCollection&lt;ViewModelBase&gt; _viewModels;
  
          public ObservableCollection&lt;ViewModelBase&gt; ViewModels {
              get {
                  if (_viewModels == null)
                      _viewModels = new ObservableCollection&lt;ViewModelBase&gt;();
  
                  return _viewModels;
              }
          }
  
          public MainWindowViewModel() {
  
              // ================================
              // Malfunctions ViewModel
              _mfRepo = new MalfunctionRepository();
  
              MalfunctionsViewModel viewModel = new MalfunctionsViewModel(_mfRepo);
  
              // ================================
              // Parts ViewModel
              //_pRepo = new PartsRepository();
  
              //PartsViewModel viewModel = new PartsViewModel(_pRepo);
  
              this.ViewModels.Add(viewModel);
          }
      }
  </code></pre>