1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace MaterialDesignThemes.Wpf
{
/// <summary>
/// Helper extensions for showing dialogs.
/// </summary>
public static class DialogHostEx
{
/// <summary>
/// Shows a dialog using the first found <see cref="DialogHost"/> in a given <see cref="Window"/>.
/// </summary>
/// <param name="window">Window on which the modal dialog should be displayed. Must contain a <see cref="DialogHost"/>.</param>
/// <param name="content"></param>
/// <exception cref="InvalidOperationException">
/// Thrown is a <see cref="DialogHost"/> is not found when conducting a depth first traversal of visual tree.
/// </exception>
/// <remarks>
/// As a depth first traversal of the window's visual tree is performed, it is not safe to use this method in a situtation where a screen has multiple <see cref="DialogHost"/>s.
/// </remarks>
/// <returns></returns>
public static async Task<object> ShowDialog(this Window window, object content)
{
return await GetFirstDialogHost(window).ShowInternal(content, null, null);
}
/// <summary>
/// Shows a dialog using the first found <see cref="DialogHost"/> in a given <see cref="Window"/>.
/// </summary>
/// <param name="window">Window on which the modal dialog should be displayed. Must contain a <see cref="DialogHost"/>.</param>
/// <param name="content">Content to show (can be a control or view model).</param>
/// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
/// <exception cref="InvalidOperationException">
/// Thrown is a <see cref="DialogHost"/> is not found when conducting a depth first traversal of visual tree.
/// </exception>
/// <remarks>
/// As a depth first traversal of the window's visual tree is performed, it is not safe to use this method in a situtation where a screen has multiple <see cref="DialogHost"/>s.
/// </remarks>
/// <returns></returns>
public static async Task<object> ShowDialog(this Window window, object content, DialogOpenedEventHandler openedEventHandler)
{
return await GetFirstDialogHost(window).ShowInternal(content, openedEventHandler, null);
}
/// <summary>
/// Shows a dialog using the first found <see cref="DialogHost"/> in a given <see cref="Window"/>.
/// </summary>
/// <param name="window">Window on which the modal dialog should be displayed. Must contain a <see cref="DialogHost"/>.</param>
/// <param name="content">Content to show (can be a control or view model).</param>
/// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
/// <exception cref="InvalidOperationException">
/// Thrown is a <see cref="DialogHost"/> is not found when conducting a depth first traversal of visual tree.
/// </exception>
/// <remarks>
/// As a depth first traversal of the window's visual tree is performed, it is not safe to use this method in a situtation where a screen has multiple <see cref="DialogHost"/>s.
/// </remarks>
/// <returns></returns>
public static async Task<object> ShowDialog(this Window window, object content, DialogClosingEventHandler closingEventHandler)
{
return await GetFirstDialogHost(window).ShowInternal(content, null, closingEventHandler);
}
/// <summary>
/// Shows a dialog using the first found <see cref="DialogHost"/> in a given <see cref="Window"/>.
/// </summary>
/// <param name="window">Window on which the modal dialog should be displayed. Must contain a <see cref="DialogHost"/>.</param>
/// <param name="content">Content to show (can be a control or view model).</param>
/// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
/// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
/// <exception cref="InvalidOperationException">
/// Thrown is a <see cref="DialogHost"/> is not found when conducting a depth first traversal of visual tree.
/// </exception>
/// <remarks>
/// As a depth first traversal of the window's visual tree is performed, it is not safe to use this method in a situtation where a screen has multiple <see cref="DialogHost"/>s.
/// </remarks>
/// <returns></returns>
public static async Task<object> ShowDialog(this Window window, object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
{
return await GetFirstDialogHost(window).ShowInternal(content, openedEventHandler, closingEventHandler);
}
/// <summary>
/// Shows a dialog using the parent/ancestor <see cref="DialogHost"/> of the a given <see cref="DependencyObject"/>.
/// </summary>
/// <param name="childDependencyObject">Dependency object which should be a visual child of a <see cref="DialogHost"/>, where the dialog will be shown.</param>
/// <param name="content">Content to show (can be a control or view model).</param>
/// <exception cref="InvalidOperationException">
/// Thrown is a <see cref="DialogHost"/> is not found when conducting a depth first traversal of visual tree.
/// </exception>
/// <returns></returns>
public static async Task<object> ShowDialog(this DependencyObject childDependencyObject, object content)
{
return await GetOwningDialogHost(childDependencyObject).ShowInternal(content, null, null);
}
/// <summary>
/// Shows a dialog using the parent/ancestor <see cref="DialogHost"/> of the a given <see cref="DependencyObject"/>.
/// </summary>
/// <param name="childDependencyObject">Dependency object which should be a visual child of a <see cref="DialogHost"/>, where the dialog will be shown.</param>
/// <param name="content">Content to show (can be a control or view model).</param>
/// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
/// <exception cref="InvalidOperationException">
/// Thrown is a <see cref="DialogHost"/> is not found when conducting a depth first traversal of visual tree.
/// </exception>
/// <returns></returns>
public static async Task<object> ShowDialog(this DependencyObject childDependencyObject, object content, DialogOpenedEventHandler openedEventHandler)
{
return await GetOwningDialogHost(childDependencyObject).ShowInternal(content, openedEventHandler, null);
}
/// <summary>
/// Shows a dialog using the parent/ancestor <see cref="DialogHost"/> of the a given <see cref="DependencyObject"/>.
/// </summary>
/// <param name="childDependencyObject">Dependency object which should be a visual child of a <see cref="DialogHost"/>, where the dialog will be shown.</param>
/// <param name="content">Content to show (can be a control or view model).</param>
/// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
/// <exception cref="InvalidOperationException">
/// Thrown is a <see cref="DialogHost"/> is not found when conducting a depth first traversal of visual tree.
/// </exception>
/// <returns></returns>
public static async Task<object> ShowDialog(this DependencyObject childDependencyObject, object content, DialogClosingEventHandler closingEventHandler)
{
return await GetOwningDialogHost(childDependencyObject).ShowInternal(content, null, closingEventHandler);
}
/// <summary>
/// Shows a dialog using the parent/ancestor <see cref="DialogHost"/> of the a given <see cref="DependencyObject"/>.
/// </summary>
/// <param name="childDependencyObject">Dependency object which should be a visual child of a <see cref="DialogHost"/>, where the dialog will be shown.</param>
/// <param name="content">Content to show (can be a control or view model).</param>
/// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
/// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
/// <exception cref="InvalidOperationException">
/// Thrown is a <see cref="DialogHost"/> is not found when conducting a depth first traversal of visual tree.
/// </exception>
/// <returns></returns>
public static async Task<object> ShowDialog(this DependencyObject childDependencyObject, object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
{
return await GetOwningDialogHost(childDependencyObject).ShowInternal(content, openedEventHandler, closingEventHandler);
}
private static DialogHost GetFirstDialogHost(Window window)
{
if (window == null) throw new ArgumentNullException(nameof(window));
var dialogHost = window.VisualDepthFirstTraversal().OfType<DialogHost>().FirstOrDefault();
if (dialogHost == null)
throw new InvalidOperationException("Unable to find a DialogHost in visual tree");
return dialogHost;
}
private static DialogHost GetOwningDialogHost(DependencyObject childDependencyObject)
{
if (childDependencyObject == null) throw new ArgumentNullException(nameof(childDependencyObject));
var dialogHost = childDependencyObject.GetVisualAncestry().OfType<DialogHost>().FirstOrDefault();
if (dialogHost == null)
throw new InvalidOperationException("Unable to find a DialogHost in visual tree ancestory");
return dialogHost;
}
}
}
|