title: WPF: Cannot get View to change dynamically using DataTemplate
body: <p>I have a problem designing my WPF application. I cannot get the Views to change dynamically. The code to call another View is contained in the Views themselves. (I am trying to implement the MVVM pattern. I do not want any code behind in the View xaml files other than assigning the DataContext. An exception is made in the xaml file of the MainWindow).</p>
  
  <p>Basically, I have a Window that contains a UserControl. The UserControl is my View and it is connected to another class serving as ViewModel through Datacontext. </p>
  
  <p>What I want to do is to dynamically change this View/ViewModel pairs contained in the Window.</p>
  
  <p>My idea was define a static property in the ViewModel of the MainWindow and store the ViewModel of the current View in it. Then I planned to use DataTemplates to automatically load a new View whenever a new ViewModel is stored in the static property.</p>
  
  <p>I decided to use a static property because the code to load another ViewModel is contained in the ViewModels itself and I needed a central point with access from everywhere.</p>
  
  <p>So far so good. My initial View loads and displays correctly. </p>
  
  <p>However, pressing a button in that View to load the next View fails although the new ViewModel is correctly assigned to the static property.
  I tried several things.</p>
  
  <p>I defined DataTriggers within the ContentControl to react to changes in the static property. No help.</p>
  
  <p>Implementing INotifyProperty and DependencyProperty failed in the end because of the static nature of the property (or I did something wrong).
  I just can’t get it to work. </p>
  
  <ol>
  <li>Do you have any ideas why this would be?</li>
  <li>Do you have an idea how I could solve my general problem of dynamically displaying Views without using a static property in my MainWindow. I believe this is causing problems and I have a notion that I am not using the most elegant method. (I do want to maintain the concept of each View holding the code to load any other View)</li>
  </ol>
  
  <p>This is a code fragment from the MainWindow:</p>
  
  <pre class="lang-cs prettyprint-override"><code>&lt;UserControl&gt;
      &lt;UserControl.Resources&gt;
          &lt;DataTemplate DataType="{x:Type vm:StartViewModel}"&gt;
              &lt;v:StartView&gt;&lt;/v:StartView&gt;
          &lt;/DataTemplate&gt;
          &lt;DataTemplate DataType="{x:Type vm:OverviewViewModel}"&gt;
              &lt;v:OverviewView&gt;&lt;/v:OverviewView&gt;
          &lt;/DataTemplate&gt;
      &lt;/UserControl.Resources&gt;
      &lt;Grid&gt;
          &lt;ContentControl Content="{Binding ActiveViewModel}"&gt;/ContentControl&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre class="lang-cs prettyprint-override"><code>DataContext = new MainViewModel();
  </code></pre>
  
  <p>MainViewModel contains the definition for the property ActiveViewModel. The constructor for the class is static. All ViewModels inherit from BaseViewModel class:</p>
  
  <pre class="lang-cs prettyprint-override"><code>private static BaseViewModel activeViewModel;
  
  static public BaseViewModel ActiveViewModel
  {
      get { return activeViewModel; }
      set { activeViewModel = value; }
  }
  </code></pre>
  
  <p>Thanks a lot for your help.</p>
  
  <p>Bye,
  Eskender</p>