title: WPF UserControl MVVM how to close a dialog
body: <p>I'm new to MVVM and run into the problem how to open a dialog and close afterwards the dialog in its own ViewModel c#-file. Search for possible solution and found nothing suitable.
  My solution looks as follows, but I'm not sure if this has some drawbacks.
  Defined a UserControl and open it with:</p>
  
  <pre><code>void ChangeDataPathExecute()
  {
      Window window = new Window
      {
          Content = new ChangeDataRootPathUserControl(),
      };
  
      window.ShowDialog(); 
  }
  </code></pre>
  
  <p>In the ViewModel of the UserControl file implement: </p>
  
  <pre><code>private void DetermineMyWindow()
  {
      foreach (Window window in App.Current.Windows)
      {
          ChangeDataRootPathUserControl uc = window.Content as ChangeDataRootPathUserControl;
          if (uc == null)
              continue;
  
          myWindow = window;
      }
  </code></pre>
  
  <p>and finally in the Close method:</p>
  
  <pre><code>void OkChangeDataRootPathExecute()
  {
      DetermineMyWindow();
  
      myWindow.Close();
  }
  </code></pre>
  
  <p>What do you think about this? Hack or good solution?
  Thanks for feedback
  Beat</p>