title: Using ViewModel as WPF DataContext Disposing problems
body: <p>Just starting out using WPF / MVVM and have a question regarding using a viewmodel as the datacontext for a wpf window.</p>
  
  <p>I'm using a view first approach and my view looks like this;</p>
  
  <pre><code>&lt;Window x:Class="TestContext.TestForm"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="TestForm" Height="300" Width="300"
      DataContext="{StaticResource testViewModel}"&gt;
      &lt;Grid&gt;
          &lt;TextBox Text="{Binding Path=Address}" Height="23" HorizontalAlignment="Left" Margin="34,44,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" /&gt;
     &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>and I create my viewmodel in code like this ;</p>
  
  <pre><code>public class ViewModelFactory
  {
      public TestViewModel CreateTestViewModel()
      {
          return new TestViewModel();
      }
  }
  </code></pre>
  
  <p>I'm using the following approach in the app.xaml to create an instance of the viewmodel whenever I instantiate the view ;</p>
  
  <pre><code>&lt;ObjectDataProvider x:Key="testViewModel" ObjectInstance="{StaticResource viewModelFactory}" MethodName="CreateTestViewModel"&gt;
  </code></pre>
  
  <p>This all works fine.</p>
  
  <p>The problem is that I only ever see the viewmodel being created once. So new instances of the view use the same instance of the viewmodel.</p>
  
  <p>I'd like to start out with a new instance of the viewmodel.</p>
  
  <p>If I create the view model manually in the forms constructer ;</p>
  
  <pre><code>public TestForm()
  {
      InitializeComponent();
      this.DataContext = new TestViewModel();
  }
  </code></pre>
  
  <p>then it works as expected.</p>
  
  <p>I'm trying to get my head around what's happening here...</p>
  
  <p>Thanks....</p>