void ConfigureDialogWindowEvents(IDialogWindow contentDialog, Action <IDialogResult> callback) { IDialogResult result = null; Action <IDialogResult> requestCloseHandler = null; requestCloseHandler = (o) => { result = o; contentDialog.Hide(); }; RoutedEventHandler loadedHandler = null; loadedHandler = (o, e) => { contentDialog.Loaded -= loadedHandler; if (contentDialog.DataContext is IDialogAware dialogAware) { dialogAware.RequestClose += requestCloseHandler; } }; contentDialog.Loaded += loadedHandler; TypedEventHandler <ContentDialog, ContentDialogClosingEventArgs> closingHandler = null; closingHandler = (o, e) => { if (contentDialog.DataContext is IDialogAware dialogAware && !dialogAware.CanCloseDialog()) { e.Cancel = true; } }; contentDialog.Closing += closingHandler; TypedEventHandler <ContentDialog, ContentDialogClosedEventArgs> closedHandler = null; closedHandler = (o, e) => { contentDialog.Closed -= closedHandler; contentDialog.Closing -= closingHandler; if (contentDialog.DataContext is IDialogAware dialogAware) { dialogAware.RequestClose -= requestCloseHandler; dialogAware.OnDialogClosed(); } if (result == null) { result = new DialogResult(); } callback?.Invoke(result); contentDialog.DataContext = null; contentDialog.Content = null; }; contentDialog.Closed += closedHandler; }