From b3108ab0fc8be816dd5d234a27e61bb57cd99bf3 Mon Sep 17 00:00:00 2001 From: Michel Michels Date: Thu, 20 Jun 2024 07:17:18 +0200 Subject: [PATCH] Fix #3503 - DialogHost overloads added (#3596) --- src/MaterialDesignThemes.Wpf/DialogHostEx.cs | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/MaterialDesignThemes.Wpf/DialogHostEx.cs b/src/MaterialDesignThemes.Wpf/DialogHostEx.cs index 27004378da..8efa5d2641 100644 --- a/src/MaterialDesignThemes.Wpf/DialogHostEx.cs +++ b/src/MaterialDesignThemes.Wpf/DialogHostEx.cs @@ -36,6 +36,16 @@ public static class DialogHostEx public static Task ShowDialog(this Window window, object content, DialogOpenedEventHandler openedEventHandler) => GetFirstDialogHost(window).ShowInternal(content, openedEventHandler, null, null); + /// + /// Shows a dialog using the first found with the supplied dialog identifier in a given . + /// + /// Window on which the modal dialog should be displayed. Must contain a . + /// Content to show (can be a control or view model). + /// of the instance where the dialog should be shown. Typically this will match an identifier set in XAML. null is allowed. + /// Task result is the parameter used to close the dialog, typically what is passed to the command. + public static Task ShowDialog(this Window window, object content, object? dialogIdentifier) + => GetFirstDialogHost(window, dialogIdentifier).ShowInternal(content, null, null, null); + /// /// Shows a dialog using the first found in a given . /// @@ -99,6 +109,19 @@ public static class DialogHostEx public static Task ShowDialog(this DependencyObject childDependencyObject, object content) => GetOwningDialogHost(childDependencyObject).ShowInternal(content, null, null, null); + /// + /// Shows a dialog using the parent/ancestor of the a given . + /// + /// Dependency object which should be a visual child of a , where the dialog will be shown. + /// Content to show (can be a control or view model). + /// of the instance where the dialog should be shown. Typically this will match an identifier set in XAML. null is allowed. + /// + /// Thrown is a is not found when conducting a depth first traversal of visual tree. + /// + /// + public static Task ShowDialog(this DependencyObject childDependencyObject, object content, object dialogIdentifier) + => GetOwningDialogHost(childDependencyObject, dialogIdentifier).ShowInternal(content, null, null, null); + /// /// Shows a dialog using the parent/ancestor of the a given . /// @@ -166,6 +189,18 @@ private static DialogHost GetFirstDialogHost(Window window) return dialogHost; } + private static DialogHost GetFirstDialogHost(Window window, object? dialogIdentifier) + { + if (window is null) throw new ArgumentNullException(nameof(window)); + + DialogHost? dialogHost = window.VisualDepthFirstTraversal().OfType().FirstOrDefault(x => x.Identifier is not null && x.Identifier.Equals(dialogIdentifier)); + + if (dialogHost is null) + throw new InvalidOperationException($"Unable to find a DialogHost with identifier '{dialogIdentifier}' in visual tree"); + + return dialogHost; + } + private static DialogHost GetOwningDialogHost(DependencyObject childDependencyObject) { if (childDependencyObject is null) throw new ArgumentNullException(nameof(childDependencyObject)); @@ -177,4 +212,15 @@ private static DialogHost GetOwningDialogHost(DependencyObject childDependencyOb return dialogHost; } + private static DialogHost GetOwningDialogHost(DependencyObject childDependencyObject, object dialogIdentifier) + { + if (childDependencyObject is null) throw new ArgumentNullException(nameof(childDependencyObject)); + + DialogHost? dialogHost = childDependencyObject.GetVisualAncestry().OfType().FirstOrDefault(x => x.Identifier is not null && x.Identifier.Equals(dialogIdentifier)); + + if (dialogHost is null) + throw new InvalidOperationException($"Unable to find a DialogHost in visual tree ancestry with identifier {dialogIdentifier}"); + + return dialogHost; + } }