private bool?ShowDialog(IViewModel a_ownerViewModel, IDialogViewModel a_viewModel, Window a_dialog) { // Create dialog and set properties a_dialog.Owner = a_ownerViewModel != null?FindOwnerWindow(a_ownerViewModel) : null; a_dialog.DataContext = a_viewModel; // Bind the closing event to the viewmodel. CancelEventHandler cancelEventHandler = (a_sender, a_e) => a_e.Cancel = !a_viewModel.DialogIsClosing(); a_dialog.Closing += cancelEventHandler; a_viewModel.DialogView = a_dialog as IDialogView; try { // Show dialog return(a_dialog.ShowDialog()); } finally { a_viewModel.DialogClosed(); a_viewModel.DialogView = null; a_dialog.Closing -= cancelEventHandler; } }