Possible Duplicate:
WPF MVVM Newbie - how should the ViewModel close the form?
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.
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.
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):
public ICommand BrowseFileFolderCommand
{
get
{
if (_browseFileFolderCommand == null)
{
_browseFileFolderCommand = new RelayCommand(o =>
{
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;
}
}
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?
The WorkspaceViewModel has the CloseCommand:
#region CloseCommand
/// <summary>
/// Returns the command that, when invoked, attempts
/// to remove this workspace from the user interface.
/// </summary>
public ICommand CloseCommand
{
get
{
if (_closeCommand == null)
_closeCommand = new RelayCommand(param => this.OnRequestClose());
return _closeCommand;
}
}
#endregion // CloseCommand
#region RequestClose [event]
/// <summary>
/// Raised when this workspace should be removed from the UI.
/// </summary>
public event EventHandler RequestClose;
void OnRequestClose()
{
EventHandler handler = this.RequestClose;
if (handler != null)
handler(this, EventArgs.Empty);
}
#endregion // RequestClose [event]
Has anyone got any ideas? Have I left out anything crucial?
Thanks,