title: WPF View sets ViewModel properties to null on closing
body: <p>I have an application where I'm displaying UserControls in a GroupBox.  To display the controls, I'm binding to a property in the ViewModel of the main form, which returns a ViewModel to be displayed.  I've got DataTemplates set up so that the form automatically knows which UserControl/View to use to display each ViewModel.</p>
  
  <p>When I display a different UserControl, I keep the ViewModel of the previous control active, but the Views are discarded automatically by WPF.</p>
  
  <p>The problem that I'm having is that when the view shuts down, any two way bindings to the properties in the ViewModel are immediately set to null, and so when I display the ViewModel again all of the values are just set to null in the UI.</p>
  
  <p>I assume this is because as part of the View closing down it disposes and clears any values in the controls it contains, and since the bindings are in place they propagate down to the ViewModel as well.</p>
  
  <p>DataTemplates in my resources</p>
  
  <pre><code>&lt;DataTemplate DataType="{x:Type vm:HomeViewModel}"&gt;
      &lt;vw:HomeView /&gt;
  &lt;/DataTemplate&gt;
  &lt;DataTemplate DataType="{x:Type vm:SettingsViewModel}"&gt;
      &lt;vw:SettingsView /&gt;
  &lt;/DataTemplate&gt;
  &lt;DataTemplate DataType="{x:Type vm:JobListViewModel}"&gt;
      &lt;vw:JobListView /&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>Code used to display user controls</p>
  
  <pre><code>&lt;GroupBox&gt;
      &lt;ContentControl  Content="{Binding Path=RightPanel}" /&gt;
  &lt;/GroupBox&gt;
  </code></pre>
  
  <p>Example of a control that I'm binding in one of the Views:</p>
  
  <pre><code>    &lt;ComboBox Name="SupervisorDropDown" ItemsSource="{Binding Path=Supervisors}" DisplayMemberPath="sgSupervisor" 
             SelectedValuePath="idSupervisor" SelectedValue="{Binding Path=SelectedSupervisorID}" /&gt;
  </code></pre>
  
  <p>and the relevant ViewModel properties:   </p>
  
  <pre><code>public ObservableCollection&lt;SupervisorsEntity&gt; Supervisors
      {
          get
          {
              return supervisors;
          }
      }
  
  public int? SelectedSupervisorID
  {
      get
      {
          return selectedSupervisorID;
      }
      set
      {
          selectedSupervisorID = value;
          this.OnPropertyChanged("SelectedSupervisorID");
      }
  }
  </code></pre>
  
  <p>Any idea on how to stop my Views nulling the values in my ViewModels?  I'm thinking that maybe I need to set the DataContext of the View to null before it closes down, but I'm not sure how to go about that with the way things are currently binding.</p>