Andy,
It took me quite awhile to figure this one out myself.
The cleanest way to accomplish this is by using the DelegateCommand (or RelayCommand) and adding an event handler in the code that creates the window with window.Show().
//===========================================================================
// Define the View
//===========================================================================
Shell window = Container.Resolve<Shell>();
//===========================================================================
// Define the ViewModel
//===========================================================================
ShellViewModel windowVM = Container.Resolve<ShellViewModel>();
//===========================================================================
// When the ViewModel asks to be closed, close the View.
//===========================================================================
EventHandler handler = null;
handler = delegate
{
windowVM.RequestClose -= handler;
window.Close();
};
windowVM.RequestClose += handler;
//===========================================================================
// Set the ViewModel as the DataContext of the View
//===========================================================================
window.DataContext = windowVM;
//===========================================================================
// Display the View
//===========================================================================
window.Show();
I then use a Composite Event to notify the window's ViewModel (not the UserControl's) that it has a request to close. The assigned subscription handler for the composite event then calls this.OnRequestClose().
In the Constructor for the ViewModel:
//subscribe to composite events
_eventAggregator.GetEvent<WindowCloseEvent>().Subscribe(WindowClose);
In the body of the ViewModel:
/// <summary>
/// Private Event handler for WindowCloseEvent.
/// </summary>
private void WindowClose(bool value)
{
// Close the View
this.OnRequestClose();
}
See Josh Smith's excellent article on MSDN about using the M-V-VM pattern with WPF at http://msdn.microsoft.com/en-us/magazine/dd419663.aspx for more information.