title: Why isn't WPF Window automatically disposed and collected after MainWindow closed?
body: <p>I have been wondering this for long and am finding the best solution. To reproduce this issue, simply:</p>
  
  <ol>
  <li>Add a new Window in a new WPF application.  </li>
  <li><p>Add the following code:</p>
  
  <pre><code>public MainWindow()
  {
      InitializeComponent();
      //add code
      Window1 w1 = new Window1();
      //following is in my actual project
      DataContext = new AllUserQueryiesViewModel(new CommonQueryTestSubWindow());
  }
  </code></pre></li>
  <li><p>Run and close MainWindow.</p></li>
  </ol>
  
  <p>By the time, I expect the application to be closed but my VS2010 seems to be still running instead. So why and how to solve it?</p>
  
  <p>---------UPDATE-----------------</p>
  
  <p>I think I'd better give my scenery in my real project, for you to better understand why I do
  this. Think of a Queries List and open query function, like that for TFS workitem queries.
  I am creating some usercontrol to do this, for others in our team to use in the same application. <strong>The Queries List and opened query are in seperate usercontrols</strong>, so the user(other programers) can place the opened query in wherever(ContentControl) they want.
  The ViewModels:</p>
  
  <pre><code>public class AllVM
  
      {
          public AllVM(IOneVMContainer container)
          {
              OneVMs = new ObservableCollection&lt;OneVM&gt;()
             {
                 new OneVM(container),
                 new OneVM(container)
             };
          }
  
          public ObservableCollection&lt;OneVM&gt; OneVMs { get; set; }
          public OneVM SelectedOneVM { get; set; }
  
          public void OpenOne()
          {
              SelectedOneVM.Open();
          }
      }
  
  public class OneVM
  {
      IOneVMContainer container;
      public OneVM(IOneVMContainer container)
      {
          this.container = container.NewInstance();
      }
      //I want to open and close the view from viewmodel
      public void Open()
      {
          container.GetContentCtl().Content = this;
          container.Show();
      }
      public void Close()
      {
          container.Close();
      }
  }
  </code></pre>
  
  <p>To open and close the view in ViewModel, I created such interface</p>
  
  <pre><code>//Is it good to use interface?
  //Maybe a better interface?
  public interface IOneVMContainer
  {
      IOneVMContainer NewInstance();
      void Show();
      void Close();
      ContentControl GetContentCtl();
  }
  </code></pre>
  
  <p>Use of above ViewModels:</p>
  
  <pre><code>//my usercontrol to hold AllVM
  public AllUC()
  {
      InitializeComponent();
      DataContext = new AllVM(new  OneContainerWindow());
  }
  
  //implement by other team members
  public partial class OneContainerWindow : Window,IOneVMContainer
  {
      public OneContainerWindow()
      {
          InitializeComponent();
      }
      public IOneVMContainer NewInstance()
      {
         return new OneContainerWindow();
      }
      public ContentControl GetContentCtl()
      {
          return contentCtl1;
      }
  }
  </code></pre>
  
  <p>So I want to know if <strong>it is inevitable to store a window in the ViewModel</strong>, which results to the main problem. I know you must have some better solution, please help.</p>