-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
DialogHost.cs
751 lines (629 loc) · 36.6 KB
/
DialogHost.cs
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using MaterialDesignThemes.Wpf.Transitions;
namespace MaterialDesignThemes.Wpf
{
/// <summary>
/// Defines how a data context is sourced for a dialog if a <see cref="FrameworkElement"/>
/// is passed as the command parameter when using <see cref="DialogHost.OpenDialogCommand"/>.
/// </summary>
public enum DialogHostOpenDialogCommandDataContextSource
{
/// <summary>
/// The data context from the sender element (typically a <see cref="Button"/>)
/// is applied to the content.
/// </summary>
SenderElement,
/// <summary>
/// The data context from the <see cref="DialogHost"/> is applied to the content.
/// </summary>
DialogHostInstance,
/// <summary>
/// The data context is explicitly set to <c>null</c>.
/// </summary>
None
}
[TemplatePart(Name = PopupPartName, Type = typeof(Popup))]
[TemplatePart(Name = PopupPartName, Type = typeof(ContentControl))]
[TemplatePart(Name = ContentCoverGridName, Type = typeof(Grid))]
[TemplateVisualState(GroupName = "PopupStates", Name = OpenStateName)]
[TemplateVisualState(GroupName = "PopupStates", Name = ClosedStateName)]
public class DialogHost : ContentControl
{
public const string PopupPartName = "PART_Popup";
public const string PopupContentPartName = "PART_PopupContentElement";
public const string ContentCoverGridName = "PART_ContentCoverGrid";
public const string OpenStateName = "Open";
public const string ClosedStateName = "Closed";
/// <summary>
/// Routed command to be used somewhere inside an instance to trigger showing of the dialog. Content can be passed to the dialog via a <see cref="Button.CommandParameter"/>.
/// </summary>
public static RoutedCommand OpenDialogCommand = new RoutedCommand();
/// <summary>
/// Routed command to be used inside dialog content to close a dialog. Use a <see cref="Button.CommandParameter"/> to indicate the result of the parameter.
/// </summary>
public static RoutedCommand CloseDialogCommand = new RoutedCommand();
private static readonly HashSet<DialogHost> LoadedInstances = new HashSet<DialogHost>();
private DialogOpenedEventHandler _asyncShowOpenedEventHandler;
private DialogClosingEventHandler _asyncShowClosingEventHandler;
private TaskCompletionSource<object> _dialogTaskCompletionSource;
private Popup _popup;
private ContentControl _popupContentControl;
private Grid _contentCoverGrid;
private DialogOpenedEventHandler _attachedDialogOpenedEventHandler;
private DialogClosingEventHandler _attachedDialogClosingEventHandler;
private IInputElement _restoreFocusDialogClose;
private IInputElement _restoreFocusWindowReactivation;
private Action _currentSnackbarMessageQueueUnPauseAction;
private Action _closeCleanUp = () => { };
static DialogHost()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DialogHost), new FrameworkPropertyMetadata(typeof(DialogHost)));
}
#region .Show overloads
/// <summary>
/// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
/// </summary>
/// <param name="content">Content to show (can be a control or view model).</param>
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
public static async Task<object> Show(object content)
{
return await Show(content, null, null);
}
/// <summary>
/// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
/// </summary>
/// <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>
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
public static async Task<object> Show(object content, DialogOpenedEventHandler openedEventHandler)
{
return await Show(content, null, openedEventHandler, null);
}
/// <summary>
/// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
/// </summary>
/// <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>
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
public static async Task<object> Show(object content, DialogClosingEventHandler closingEventHandler)
{
return await Show(content, null, null, closingEventHandler);
}
/// <summary>
/// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
/// </summary>
/// <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>
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
public static async Task<object> Show(object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
{
return await Show(content, null, openedEventHandler, closingEventHandler);
}
/// <summary>
/// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
/// </summary>
/// <param name="content">Content to show (can be a control or view model).</param>
/// <param name="dialogIdentifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifer set in XAML. <c>null</c> is allowed.</param>
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
public static async Task<object> Show(object content, object dialogIdentifier)
{
return await Show(content, dialogIdentifier, null, null);
}
/// <summary>
/// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
/// </summary>
/// <param name="content">Content to show (can be a control or view model).</param>
/// <param name="dialogIdentifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifer set in XAML. <c>null</c> is allowed.</param>
/// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
public static Task<object> Show(object content, object dialogIdentifier, DialogOpenedEventHandler openedEventHandler)
{
return Show(content, dialogIdentifier, openedEventHandler, null);
}
/// <summary>
/// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
/// </summary>
/// <param name="content">Content to show (can be a control or view model).</param>
/// <param name="dialogIdentifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifer set in XAML. <c>null</c> is allowed.</param>
/// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
public static Task<object> Show(object content, object dialogIdentifier, DialogClosingEventHandler closingEventHandler)
{
return Show(content, dialogIdentifier, null, closingEventHandler);
}
/// <summary>
/// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
/// </summary>
/// <param name="content">Content to show (can be a control or view model).</param>
/// <param name="dialogIdentifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifer set in XAML. <c>null</c> is allowed.</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>
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
public static async Task<object> Show(object content, object dialogIdentifier, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
{
if (content == null) throw new ArgumentNullException(nameof(content));
if (LoadedInstances.Count == 0)
throw new InvalidOperationException("No loaded DialogHost instances.");
LoadedInstances.First().Dispatcher.VerifyAccess();
var targets = LoadedInstances.Where(dh => dialogIdentifier == null || Equals(dh.Identifier, dialogIdentifier)).ToList();
if (targets.Count == 0)
throw new InvalidOperationException($"No loaded DialogHost have an {nameof(Identifier)} property matching {nameof(dialogIdentifier)} argument.");
if (targets.Count > 1)
throw new InvalidOperationException("Multiple viable DialogHosts. Specify a unique Identifier on each DialogHost, especially where multiple Windows are a concern.");
return await targets[0].ShowInternal(content, openedEventHandler, closingEventHandler);
}
internal async Task<object> ShowInternal(object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
{
if (IsOpen)
throw new InvalidOperationException("DialogHost is already open.");
_dialogTaskCompletionSource = new TaskCompletionSource<object>();
AssertTargetableContent();
DialogContent = content;
_asyncShowOpenedEventHandler = openedEventHandler;
_asyncShowClosingEventHandler = closingEventHandler;
SetCurrentValue(IsOpenProperty, true);
object result = await _dialogTaskCompletionSource.Task;
_asyncShowOpenedEventHandler = null;
_asyncShowClosingEventHandler = null;
return result;
}
#endregion
public DialogHost()
{
Loaded += OnLoaded;
Unloaded += OnUnloaded;
CommandBindings.Add(new CommandBinding(CloseDialogCommand, CloseDialogHandler, CloseDialogCanExecute));
CommandBindings.Add(new CommandBinding(OpenDialogCommand, OpenDialogHandler));
}
public static readonly DependencyProperty IdentifierProperty = DependencyProperty.Register(
nameof(Identifier), typeof(object), typeof(DialogHost), new PropertyMetadata(default(object)));
/// <summary>
/// Identifier which is used in conjunction with <see cref="Show(object)"/> to determine where a dialog should be shown.
/// </summary>
public object Identifier
{
get { return GetValue(IdentifierProperty); }
set { SetValue(IdentifierProperty, value); }
}
public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register(
nameof(IsOpen), typeof(bool), typeof(DialogHost), new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsOpenPropertyChangedCallback));
private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var dialogHost = (DialogHost)dependencyObject;
if (dialogHost._popupContentControl != null)
ValidationAssist.SetSuppress(dialogHost._popupContentControl, !dialogHost.IsOpen);
VisualStateManager.GoToState(dialogHost, dialogHost.SelectState(), !TransitionAssist.GetDisableTransitions(dialogHost));
if (dialogHost.IsOpen)
{
WatchWindowActivation(dialogHost);
dialogHost._currentSnackbarMessageQueueUnPauseAction = dialogHost.SnackbarMessageQueue?.Pause();
}
else
{
dialogHost._attachedDialogClosingEventHandler = null;
if (dialogHost._currentSnackbarMessageQueueUnPauseAction != null)
{
dialogHost._currentSnackbarMessageQueueUnPauseAction();
dialogHost._currentSnackbarMessageQueueUnPauseAction = null;
}
dialogHost.CurrentSession.IsEnded = true;
dialogHost.CurrentSession = null;
dialogHost._closeCleanUp();
//NB: _dialogTaskCompletionSource is only set in the case where the dialog is shown with Show
//To get into this case you need to display the dialog with Show and then hide it by setting IsOpen to false
//Setting this here ensures the other
dialogHost._dialogTaskCompletionSource?.TrySetResult(null);
// Don't attempt to Invoke if _restoreFocusDialogClose hasn't been assigned yet. Can occur
// if the MainWindow has started up minimized. Even when Show() has been called, this doesn't
// seem to have been set.
dialogHost.Dispatcher.InvokeAsync(() => dialogHost._restoreFocusDialogClose?.Focus(), DispatcherPriority.Input);
return;
}
dialogHost.CurrentSession = new DialogSession(dialogHost);
var window = Window.GetWindow(dialogHost);
dialogHost._restoreFocusDialogClose = window != null ? FocusManager.GetFocusedElement(window) : null;
//multiple ways of calling back that the dialog has opened:
// * routed event
// * the attached property (which should be applied to the button which opened the dialog
// * straight forward dependency property
// * handler provided to the async show method
var dialogOpenedEventArgs = new DialogOpenedEventArgs(dialogHost.CurrentSession, DialogOpenedEvent);
dialogHost.OnDialogOpened(dialogOpenedEventArgs);
dialogHost._attachedDialogOpenedEventHandler?.Invoke(dialogHost, dialogOpenedEventArgs);
dialogHost.DialogOpenedCallback?.Invoke(dialogHost, dialogOpenedEventArgs);
dialogHost._asyncShowOpenedEventHandler?.Invoke(dialogHost, dialogOpenedEventArgs);
dialogHost.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
CommandManager.InvalidateRequerySuggested();
UIElement child = dialogHost.FocusPopup();
if (child != null)
{
//https://github.com/ButchersBoy/MaterialDesignInXamlToolkit/issues/187
//totally not happy about this, but on immediate validation we can get some weird looking stuff...give WPF a kick to refresh...
Task.Delay(300).ContinueWith(t => child.Dispatcher.BeginInvoke(new Action(() => child.InvalidateVisual())));
}
}));
}
/// <summary>
/// Returns a DialogSession for the currently open dialog for managing it programmatically. If no dialog is open, CurrentSession will return null
/// </summary>
public DialogSession CurrentSession { get; private set; }
public bool IsOpen
{
get { return (bool)GetValue(IsOpenProperty); }
set { SetValue(IsOpenProperty, value); }
}
public static readonly DependencyProperty DialogContentProperty = DependencyProperty.Register(
nameof(DialogContent), typeof(object), typeof(DialogHost), new PropertyMetadata(default(object)));
public object DialogContent
{
get { return GetValue(DialogContentProperty); }
set { SetValue(DialogContentProperty, value); }
}
public static readonly DependencyProperty DialogContentTemplateProperty = DependencyProperty.Register(
nameof(DialogContentTemplate), typeof(DataTemplate), typeof(DialogHost), new PropertyMetadata(default(DataTemplate)));
public DataTemplate DialogContentTemplate
{
get { return (DataTemplate)GetValue(DialogContentTemplateProperty); }
set { SetValue(DialogContentTemplateProperty, value); }
}
public static readonly DependencyProperty DialogContentTemplateSelectorProperty = DependencyProperty.Register(
nameof(DialogContentTemplateSelector), typeof(DataTemplateSelector), typeof(DialogHost), new PropertyMetadata(default(DataTemplateSelector)));
public DataTemplateSelector DialogContentTemplateSelector
{
get { return (DataTemplateSelector)GetValue(DialogContentTemplateSelectorProperty); }
set { SetValue(DialogContentTemplateSelectorProperty, value); }
}
public static readonly DependencyProperty DialogContentStringFormatProperty = DependencyProperty.Register(
nameof(DialogContentStringFormat), typeof(string), typeof(DialogHost), new PropertyMetadata(default(string)));
public string DialogContentStringFormat
{
get { return (string)GetValue(DialogContentStringFormatProperty); }
set { SetValue(DialogContentStringFormatProperty, value); }
}
public static readonly DependencyProperty DialogMarginProperty = DependencyProperty.Register(
"DialogMargin", typeof(Thickness), typeof(DialogHost), new PropertyMetadata(default(Thickness)));
public Thickness DialogMargin
{
get { return (Thickness)GetValue(DialogMarginProperty); }
set { SetValue(DialogMarginProperty, value); }
}
public static readonly DependencyProperty OpenDialogCommandDataContextSourceProperty = DependencyProperty.Register(
nameof(OpenDialogCommandDataContextSource), typeof(DialogHostOpenDialogCommandDataContextSource), typeof(DialogHost), new PropertyMetadata(default(DialogHostOpenDialogCommandDataContextSource)));
/// <summary>
/// Defines how a data context is sourced for a dialog if a <see cref="FrameworkElement"/>
/// is passed as the command parameter when using <see cref="DialogHost.OpenDialogCommand"/>.
/// </summary>
public DialogHostOpenDialogCommandDataContextSource OpenDialogCommandDataContextSource
{
get { return (DialogHostOpenDialogCommandDataContextSource)GetValue(OpenDialogCommandDataContextSourceProperty); }
set { SetValue(OpenDialogCommandDataContextSourceProperty, value); }
}
public static readonly DependencyProperty CloseOnClickAwayProperty = DependencyProperty.Register(
"CloseOnClickAway", typeof(bool), typeof(DialogHost), new PropertyMetadata(default(bool)));
/// <summary>
/// Indicates whether the dialog will close if the user clicks off the dialog, on the obscured background.
/// </summary>
public bool CloseOnClickAway
{
get { return (bool)GetValue(CloseOnClickAwayProperty); }
set { SetValue(CloseOnClickAwayProperty, value); }
}
public static readonly DependencyProperty CloseOnClickAwayParameterProperty = DependencyProperty.Register(
"CloseOnClickAwayParameter", typeof(object), typeof(DialogHost), new PropertyMetadata(default(object)));
/// <summary>
/// Parameter to provide to close handlers if an close due to click away is instigated.
/// </summary>
public object CloseOnClickAwayParameter
{
get { return GetValue(CloseOnClickAwayParameterProperty); }
set { SetValue(CloseOnClickAwayParameterProperty, value); }
}
public static readonly DependencyProperty SnackbarMessageQueueProperty = DependencyProperty.Register(
"SnackbarMessageQueue", typeof(SnackbarMessageQueue), typeof(DialogHost), new PropertyMetadata(default(SnackbarMessageQueue), SnackbarMessageQueuePropertyChangedCallback));
private static void SnackbarMessageQueuePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var dialogHost = (DialogHost)dependencyObject;
if (dialogHost._currentSnackbarMessageQueueUnPauseAction != null)
{
dialogHost._currentSnackbarMessageQueueUnPauseAction();
dialogHost._currentSnackbarMessageQueueUnPauseAction = null;
}
if (!dialogHost.IsOpen) return;
var snackbarMessageQueue = dependencyPropertyChangedEventArgs.NewValue as SnackbarMessageQueue;
dialogHost._currentSnackbarMessageQueueUnPauseAction = snackbarMessageQueue?.Pause();
}
/// <summary>
/// Allows association of a snackbar, so that notifications can be paused whilst a dialog is being displayed.
/// </summary>
public SnackbarMessageQueue SnackbarMessageQueue
{
get { return (SnackbarMessageQueue)GetValue(SnackbarMessageQueueProperty); }
set { SetValue(SnackbarMessageQueueProperty, value); }
}
public static readonly DependencyProperty DialogThemeProperty =
DependencyProperty.Register(nameof(DialogTheme), typeof(BaseTheme), typeof(DialogHost), new PropertyMetadata(default(BaseTheme)));
/// <summary>
/// Set the theme (light/dark) for the dialog.
/// </summary>
public BaseTheme DialogTheme
{
get { return (BaseTheme)GetValue(DialogThemeProperty); }
set { SetValue(DialogThemeProperty, value); }
}
public static readonly DependencyProperty PopupStyleProperty = DependencyProperty.Register(
nameof(PopupStyle), typeof(Style), typeof(DialogHost), new PropertyMetadata(default(Style)));
public Style PopupStyle
{
get { return (Style)GetValue(PopupStyleProperty); }
set { SetValue(PopupStyleProperty, value); }
}
public static readonly DependencyProperty OverlayBackgroundProperty = DependencyProperty.Register(
nameof(OverlayBackground), typeof(Brush), typeof(DialogHost), new PropertyMetadata(Brushes.Black));
/// <summary>
/// Represents the overlay brush that is used to dim the background behind the dialog
/// </summary>
public Brush OverlayBackground
{
get { return (Brush)GetValue(OverlayBackgroundProperty); }
set { SetValue(OverlayBackgroundProperty, value); }
}
public override void OnApplyTemplate()
{
if (_contentCoverGrid != null)
_contentCoverGrid.MouseLeftButtonUp -= ContentCoverGridOnMouseLeftButtonUp;
_popup = GetTemplateChild(PopupPartName) as Popup;
_popupContentControl = GetTemplateChild(PopupContentPartName) as ContentControl;
_contentCoverGrid = GetTemplateChild(ContentCoverGridName) as Grid;
if (_contentCoverGrid != null)
_contentCoverGrid.MouseLeftButtonUp += ContentCoverGridOnMouseLeftButtonUp;
VisualStateManager.GoToState(this, SelectState(), false);
base.OnApplyTemplate();
}
#region open dialog events/callbacks
public static readonly RoutedEvent DialogOpenedEvent =
EventManager.RegisterRoutedEvent(
"DialogOpened",
RoutingStrategy.Bubble,
typeof(DialogOpenedEventHandler),
typeof(DialogHost));
/// <summary>
/// Raised when a dialog is opened.
/// </summary>
public event DialogOpenedEventHandler DialogOpened
{
add { AddHandler(DialogOpenedEvent, value); }
remove { RemoveHandler(DialogOpenedEvent, value); }
}
/// <summary>
/// Attached property which can be used on the <see cref="Button"/> which instigated the <see cref="OpenDialogCommand"/> to process the event.
/// </summary>
public static readonly DependencyProperty DialogOpenedAttachedProperty = DependencyProperty.RegisterAttached(
"DialogOpenedAttached", typeof(DialogOpenedEventHandler), typeof(DialogHost), new PropertyMetadata(default(DialogOpenedEventHandler)));
public static void SetDialogOpenedAttached(DependencyObject element, DialogOpenedEventHandler value)
{
element.SetValue(DialogOpenedAttachedProperty, value);
}
public static DialogOpenedEventHandler GetDialogOpenedAttached(DependencyObject element)
{
return (DialogOpenedEventHandler)element.GetValue(DialogOpenedAttachedProperty);
}
public static readonly DependencyProperty DialogOpenedCallbackProperty = DependencyProperty.Register(
nameof(DialogOpenedCallback), typeof(DialogOpenedEventHandler), typeof(DialogHost), new PropertyMetadata(default(DialogOpenedEventHandler)));
/// <summary>
/// Callback fired when the <see cref="DialogOpened"/> event is fired, allowing the event to be processed from a binding/view model.
/// </summary>
public DialogOpenedEventHandler DialogOpenedCallback
{
get { return (DialogOpenedEventHandler)GetValue(DialogOpenedCallbackProperty); }
set { SetValue(DialogOpenedCallbackProperty, value); }
}
protected void OnDialogOpened(DialogOpenedEventArgs eventArgs)
{
RaiseEvent(eventArgs);
}
#endregion
#region close dialog events/callbacks
public static readonly RoutedEvent DialogClosingEvent =
EventManager.RegisterRoutedEvent(
"DialogClosing",
RoutingStrategy.Bubble,
typeof(DialogClosingEventHandler),
typeof(DialogHost));
/// <summary>
/// Raised just before a dialog is closed.
/// </summary>
public event DialogClosingEventHandler DialogClosing
{
add { AddHandler(DialogClosingEvent, value); }
remove { RemoveHandler(DialogClosingEvent, value); }
}
/// <summary>
/// Attached property which can be used on the <see cref="Button"/> which instigated the <see cref="OpenDialogCommand"/> to process the closing event.
/// </summary>
public static readonly DependencyProperty DialogClosingAttachedProperty = DependencyProperty.RegisterAttached(
"DialogClosingAttached", typeof(DialogClosingEventHandler), typeof(DialogHost), new PropertyMetadata(default(DialogClosingEventHandler)));
public static void SetDialogClosingAttached(DependencyObject element, DialogClosingEventHandler value)
{
element.SetValue(DialogClosingAttachedProperty, value);
}
public static DialogClosingEventHandler GetDialogClosingAttached(DependencyObject element)
{
return (DialogClosingEventHandler)element.GetValue(DialogClosingAttachedProperty);
}
public static readonly DependencyProperty DialogClosingCallbackProperty = DependencyProperty.Register(
nameof(DialogClosingCallback), typeof(DialogClosingEventHandler), typeof(DialogHost), new PropertyMetadata(default(DialogClosingEventHandler)));
/// <summary>
/// Callback fired when the <see cref="DialogClosing"/> event is fired, allowing the event to be processed from a binding/view model.
/// </summary>
public DialogClosingEventHandler DialogClosingCallback
{
get { return (DialogClosingEventHandler)GetValue(DialogClosingCallbackProperty); }
set { SetValue(DialogClosingCallbackProperty, value); }
}
protected void OnDialogClosing(DialogClosingEventArgs eventArgs)
{
RaiseEvent(eventArgs);
}
#endregion
internal void AssertTargetableContent()
{
var existingBinding = BindingOperations.GetBindingExpression(this, DialogContentProperty);
if (existingBinding != null)
throw new InvalidOperationException(
"Content cannot be passed to a dialog via the OpenDialog if DialogContent already has a binding.");
}
internal void Close(object parameter)
{
var dialogClosingEventArgs = new DialogClosingEventArgs(CurrentSession, parameter, DialogClosingEvent);
CurrentSession.IsEnded = true;
//multiple ways of calling back that the dialog is closing:
// * routed event
// * the attached property (which should be applied to the button which opened the dialog
// * straight forward dependency property
// * handler provided to the async show method
OnDialogClosing(dialogClosingEventArgs);
_attachedDialogClosingEventHandler?.Invoke(this, dialogClosingEventArgs);
DialogClosingCallback?.Invoke(this, dialogClosingEventArgs);
_asyncShowClosingEventHandler?.Invoke(this, dialogClosingEventArgs);
if (!dialogClosingEventArgs.IsCancelled)
{
_dialogTaskCompletionSource?.TrySetResult(parameter);
SetCurrentValue(IsOpenProperty, false);
}
else
{
CurrentSession.IsEnded = false;
}
}
/// <summary>
/// Attempts to focus the content of a popup.
/// </summary>
/// <returns>The popup content.</returns>
internal UIElement FocusPopup()
{
var child = _popup?.Child;
if (child == null) return null;
CommandManager.InvalidateRequerySuggested();
var focusable = child.VisualDepthFirstTraversal().OfType<UIElement>().FirstOrDefault(ui => ui.Focusable && ui.IsVisible);
focusable?.Dispatcher.InvokeAsync(() =>
{
if (!focusable.Focus()) return;
focusable.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
}, DispatcherPriority.Background);
return child;
}
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{
var window = Window.GetWindow(this);
if (window != null && !window.IsActive)
window.Activate();
base.OnPreviewMouseDown(e);
}
private void ContentCoverGridOnMouseLeftButtonUp(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
if (CloseOnClickAway && CurrentSession != null)
Close(CloseOnClickAwayParameter);
}
private void OpenDialogHandler(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
{
if (executedRoutedEventArgs.Handled) return;
if (executedRoutedEventArgs.OriginalSource is DependencyObject dependencyObject)
{
_attachedDialogOpenedEventHandler = GetDialogOpenedAttached(dependencyObject);
_attachedDialogClosingEventHandler = GetDialogClosingAttached(dependencyObject);
}
if (executedRoutedEventArgs.Parameter != null)
{
AssertTargetableContent();
if (_popupContentControl != null)
{
switch (OpenDialogCommandDataContextSource)
{
case DialogHostOpenDialogCommandDataContextSource.SenderElement:
_popupContentControl.DataContext =
(executedRoutedEventArgs.OriginalSource as FrameworkElement)?.DataContext;
break;
case DialogHostOpenDialogCommandDataContextSource.DialogHostInstance:
_popupContentControl.DataContext = DataContext;
break;
case DialogHostOpenDialogCommandDataContextSource.None:
_popupContentControl.DataContext = null;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
DialogContent = executedRoutedEventArgs.Parameter;
}
SetCurrentValue(IsOpenProperty, true);
executedRoutedEventArgs.Handled = true;
}
private void CloseDialogCanExecute(object sender, CanExecuteRoutedEventArgs canExecuteRoutedEventArgs)
{
canExecuteRoutedEventArgs.CanExecute = CurrentSession != null;
}
private void CloseDialogHandler(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
{
if (executedRoutedEventArgs.Handled) return;
Close(executedRoutedEventArgs.Parameter);
executedRoutedEventArgs.Handled = true;
}
private string SelectState()
{
return IsOpen ? OpenStateName : ClosedStateName;
}
private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
{
LoadedInstances.Remove(this);
SetCurrentValue(IsOpenProperty, false);
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
LoadedInstances.Add(this);
}
private static void WatchWindowActivation(DialogHost dialogHost)
{
var window = Window.GetWindow(dialogHost);
if (window != null)
{
window.Activated += dialogHost.WindowOnActivated;
window.Deactivated += dialogHost.WindowOnDeactivated;
dialogHost._closeCleanUp = () =>
{
window.Activated -= dialogHost.WindowOnActivated;
window.Deactivated -= dialogHost.WindowOnDeactivated;
};
}
else
{
dialogHost._closeCleanUp = () => { };
}
}
private void WindowOnDeactivated(object sender, EventArgs eventArgs)
{
_restoreFocusWindowReactivation = _popup != null ? FocusManager.GetFocusedElement((Window)sender) : null;
}
private void WindowOnActivated(object sender, EventArgs eventArgs)
{
if (_restoreFocusWindowReactivation != null)
{
Dispatcher.BeginInvoke(new Action(() =>
{
Keyboard.Focus(_restoreFocusWindowReactivation);
}));
}
}
}
}