title: Opening WPF Window MVVM and Closing it
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/501886/wpf-mvvm-newbie-how-should-the-viewmodel-close-the-form">WPF MVVM Newbie - how should the ViewModel close the form?</a>  </p>
  </blockquote>
  
  
  
  <p>I've searched around stackoverflow and I don't think the answers given apply to mine or I can't fathom out how to apply them.</p>
  
  <p>I have a bog standard MVVM WPF application. The MVVM parts consist of a RelayCommand class and a ViewModelBase class and a WorkspaceViewModel class extending ViewModelBase.</p>
  
  <p>I have two windows, the MainWindow and the CustomMessageBox window (which actually provides a the user with a question and two answers). I use this code in MainWindow to open the CustomMessageBox (second window):</p>
  
  <pre><code>public ICommand BrowseFileFolderCommand
      {
          get
          {
              if (_browseFileFolderCommand == null)
              {
                  _browseFileFolderCommand = new RelayCommand(o =&gt;
                      {
                          var messageViewModel = new MessageBoxViewModel("Add a Folder or File", "What do you wish to add, folder or file?", "Folder", "File");
                          var choice = new CustomMessageBox()
                          {
                              DataContext = messageViewModel
                          };
                          choice.ShowDialog();
  
                          if (messageViewModel.CustomMessageBoxDialogResult == DialogResult.Yes)
                          {
                              switch (messageViewModel.ChosenEntity)
                              {
                                  case SelectedAnswer.Answer1:
                                      // Get folder shizz
                                      break;
                                  case SelectedAnswer.Answer2:
                                      // Get file shizz
                                      break;
                                  default:
                                      break;
                              }
                          }
                      }, null);
              }
              return _browseFileFolderCommand;
          }
      }
  </code></pre>
  
  <p>Once the CustomMessageBox has been launched I cannot close it with the CloseCommand. When I try and debug the loading of CustomMessageBox, it's seems all the ICommands are fired off before I press anything?</p>
  
  <p>The WorkspaceViewModel has the CloseCommand:</p>
  
  <pre><code> #region CloseCommand
  
      /// &lt;summary&gt;
      /// Returns the command that, when invoked, attempts
      /// to remove this workspace from the user interface.
      /// &lt;/summary&gt;
      public ICommand CloseCommand
      {
          get
          {
              if (_closeCommand == null)
                  _closeCommand = new RelayCommand(param =&gt; this.OnRequestClose());
  
              return _closeCommand;
          }
      }
  
      #endregion // CloseCommand
  
      #region RequestClose [event]
  
      /// &lt;summary&gt;
      /// Raised when this workspace should be removed from the UI.
      /// &lt;/summary&gt;
      public event EventHandler RequestClose;
  
      void OnRequestClose()
      {
          EventHandler handler = this.RequestClose;
          if (handler != null)
              handler(this, EventArgs.Empty);
      }
  
      #endregion // RequestClose [event]
  </code></pre>
  
  <p>Has anyone got any ideas? Have I left out anything crucial?</p>
  
  <p>Thanks,</p>