title: Two Views in one ViewModel (WPF/MVVM)
body: <p>PROBLEM: Use one single viewModel with two different views.</p>
  
  <p>I have a <code>Window</code> with a control <code>ContentControl</code> which is binded to a property in the <code>DataContext</code>, called <code>Object MainContent {get;set;}</code>. Base on a navigationType <code>enum</code> property, I assign other ViewModels to it to show the correct <code>UserControl</code>.</p>
  
  <p>I need to merge two views into one ViewModel, and because I'm assigning a ViewModel to the <code>ContentControl</code> mentioned before, the <code>TemplateSelector</code> is not able to identify which is the correct view as both shares the same viewModel</p>
  
  <p>If I assign the view instead the ViewModel to the <code>ContentControl</code>, the correct view is shown, however, non of the commands works.</p>
  
  <p>Any Help? Thanks in advance.
  <br>
  <br>
  <br>
  <br>
  <br>
  SOLUTION: based on @mm8 answer and <a href="https://stackoverflow.com/a/5310213/2315752">https://stackoverflow.com/a/5310213/2315752</a>:</p>
  
  <p>ManagePatientViewModel.cs</p>
  
  <pre><code>public class ManagePatientViewModel : ViewModelBase
  {
      public ManagePatientViewModel (MainWindowViewModel inMainVM) : base(inMainVM) {}
  } 
  </code></pre>
  
  <p>ViewHelper.cs</p>
  
  <pre><code>public enum ViewState
  {
      SEARCH,
      CREATE,
  }
  </code></pre>
  
  <p>MainWindowViewModel.cs</p>
  
  <pre><code>public ViewState State {get;set;}
  public ManagePatientViewModel VM {get;set;}
  
  private void ChangeView(ViewState inState)
  {
      State = inState;
  
      // This is need to force the update of Content.
      var copy = VM;
      MainContent = null;
      MainContent = copy;
  }
  
  public void NavigateTo (NavigationType inNavigation)
  {
      switch (inNavigationType)
      {
          case NavigationType.CREATE_PATIENT:
              ChangeView(ViewState.CREATE);
              break;
          case NavigationType.SEARCH_PATIENT:
              ChangeView(ViewState.SEARCH);
              break;
          default:
              throw new ArgumentOutOfRangeException(nameof(inNavigationType), inNavigationType, null);
      }
  }
  </code></pre>
  
  <p>MainWindow.xaml</p>
  
  <pre><code>&lt;DataTemplate x:Key="CreateTemplate"&gt;
          &lt;views:CreateView /&gt;
  &lt;/DataTemplate&gt;
  
  &lt;DataTemplate x:Key="SearchTemplate"&gt;
          &lt;views:SearchView/&gt;
  &lt;/DataTemplate&gt;
  
  &lt;TemplateSelector x:Key="ViewSelector"
      SearchViewTemplate="{StaticResource SearchTemplate}"
      CreateViewTemplate="{StaticResource CreateTemplate}"/&gt;
  
  &lt;ContentControl
          Grid.Row="1"
          Content="{Binding MainContent}"
          ContentTemplateSelector="{StaticResource ViewSelector}" /&gt;
  </code></pre>
  
  <p>TemplateSelector.cs</p>
  
  <pre><code>public class TemplateSelector : DataTemplateSelector
  {
      public DataTemplate SearchViewTemplate {get;set;}
      public DataTemplate CreateViewTemplate {get;set;}
  }
  
  public override DataTemplate SelectTemplate(object item, DependencyObject container)
  {
      if (!(item is SelectLesionViewModel vm))
      {
          return null;
      }
  
      switch (vm.ViewType)
      {
          case ViewState.CREATE:
              return CreateViewTemplate;
          case ViewState.SEARCH:
              return SearchViewTemplate;
          default:
              return null;
          }
      }
  }
  </code></pre>