Skip to content

Commit b3108ab

Browse files
Fix #3503 - DialogHost overloads added (#3596)
1 parent f8c1eed commit b3108ab

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/MaterialDesignThemes.Wpf/DialogHostEx.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public static class DialogHostEx
3636
public static Task<object?> ShowDialog(this Window window, object content, DialogOpenedEventHandler openedEventHandler)
3737
=> GetFirstDialogHost(window).ShowInternal(content, openedEventHandler, null, null);
3838

39+
/// <summary>
40+
/// Shows a dialog using the first found <see cref="DialogHost"/> with the supplied dialog identifier in a given <see cref="Window"/>.
41+
/// </summary>
42+
/// <param name="window">Window on which the modal dialog should be displayed. Must contain a <see cref="DialogHost"/>.</param>
43+
/// <param name="content">Content to show (can be a control or view model).</param>
44+
/// <param name="dialogIdentifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifier set in XAML. <c>null</c> is allowed.</param>
45+
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
46+
public static Task<object?> ShowDialog(this Window window, object content, object? dialogIdentifier)
47+
=> GetFirstDialogHost(window, dialogIdentifier).ShowInternal(content, null, null, null);
48+
3949
/// <summary>
4050
/// Shows a dialog using the first found <see cref="DialogHost"/> in a given <see cref="Window"/>.
4151
/// </summary>
@@ -99,6 +109,19 @@ public static class DialogHostEx
99109
public static Task<object?> ShowDialog(this DependencyObject childDependencyObject, object content)
100110
=> GetOwningDialogHost(childDependencyObject).ShowInternal(content, null, null, null);
101111

112+
/// <summary>
113+
/// Shows a dialog using the parent/ancestor <see cref="DialogHost"/> of the a given <see cref="DependencyObject"/>.
114+
/// </summary>
115+
/// <param name="childDependencyObject">Dependency object which should be a visual child of a <see cref="DialogHost"/>, where the dialog will be shown.</param>
116+
/// <param name="content">Content to show (can be a control or view model).</param>
117+
/// <param name="dialogIdentifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifier set in XAML. <c>null</c> is allowed.</param>
118+
/// <exception cref="InvalidOperationException">
119+
/// Thrown is a <see cref="DialogHost"/> is not found when conducting a depth first traversal of visual tree.
120+
/// </exception>
121+
/// <returns></returns>
122+
public static Task<object?> ShowDialog(this DependencyObject childDependencyObject, object content, object dialogIdentifier)
123+
=> GetOwningDialogHost(childDependencyObject, dialogIdentifier).ShowInternal(content, null, null, null);
124+
102125
/// <summary>
103126
/// Shows a dialog using the parent/ancestor <see cref="DialogHost"/> of the a given <see cref="DependencyObject"/>.
104127
/// </summary>
@@ -166,6 +189,18 @@ private static DialogHost GetFirstDialogHost(Window window)
166189
return dialogHost;
167190
}
168191

192+
private static DialogHost GetFirstDialogHost(Window window, object? dialogIdentifier)
193+
{
194+
if (window is null) throw new ArgumentNullException(nameof(window));
195+
196+
DialogHost? dialogHost = window.VisualDepthFirstTraversal().OfType<DialogHost>().FirstOrDefault(x => x.Identifier is not null && x.Identifier.Equals(dialogIdentifier));
197+
198+
if (dialogHost is null)
199+
throw new InvalidOperationException($"Unable to find a DialogHost with identifier '{dialogIdentifier}' in visual tree");
200+
201+
return dialogHost;
202+
}
203+
169204
private static DialogHost GetOwningDialogHost(DependencyObject childDependencyObject)
170205
{
171206
if (childDependencyObject is null) throw new ArgumentNullException(nameof(childDependencyObject));
@@ -177,4 +212,15 @@ private static DialogHost GetOwningDialogHost(DependencyObject childDependencyOb
177212

178213
return dialogHost;
179214
}
215+
private static DialogHost GetOwningDialogHost(DependencyObject childDependencyObject, object dialogIdentifier)
216+
{
217+
if (childDependencyObject is null) throw new ArgumentNullException(nameof(childDependencyObject));
218+
219+
DialogHost? dialogHost = childDependencyObject.GetVisualAncestry().OfType<DialogHost>().FirstOrDefault(x => x.Identifier is not null && x.Identifier.Equals(dialogIdentifier));
220+
221+
if (dialogHost is null)
222+
throw new InvalidOperationException($"Unable to find a DialogHost in visual tree ancestry with identifier {dialogIdentifier}");
223+
224+
return dialogHost;
225+
}
180226
}

0 commit comments

Comments
 (0)