@@ -36,12 +36,14 @@ public enum DialogHostOpenDialogCommandDataContextSource
36
36
37
37
[ TemplatePart ( Name = PopupPartName , Type = typeof ( Popup ) ) ]
38
38
[ TemplatePart ( Name = PopupPartName , Type = typeof ( ContentControl ) ) ]
39
+ [ TemplatePart ( Name = ContentCoverGridName , Type = typeof ( Grid ) ) ]
39
40
[ TemplateVisualState ( GroupName = "PopupStates" , Name = OpenStateName ) ]
40
41
[ TemplateVisualState ( GroupName = "PopupStates" , Name = ClosedStateName ) ]
41
42
public class DialogHost : ContentControl
42
43
{
43
44
public const string PopupPartName = "PART_Popup" ;
44
45
public const string PopupContentPartName = "PART_PopupContentElement" ;
46
+ public const string ContentCoverGridName = "PART_ContentCoverGrid" ;
45
47
public const string OpenStateName = "Open" ;
46
48
public const string ClosedStateName = "Closed" ;
47
49
@@ -62,6 +64,7 @@ public class DialogHost : ContentControl
62
64
63
65
private Popup _popup ;
64
66
private ContentControl _popupContentControl ;
67
+ private Grid _contentCoverGrid ;
65
68
private DialogSession _session ;
66
69
private DialogOpenedEventHandler _attachedDialogOpenedEventHandler ;
67
70
private DialogClosingEventHandler _attachedDialogClosingEventHandler ;
@@ -330,10 +333,41 @@ public DialogHostOpenDialogCommandDataContextSource OpenDialogCommandDataContext
330
333
set { SetValue ( OpenDialogCommandDataContextSourceProperty , value ) ; }
331
334
}
332
335
336
+ public static readonly DependencyProperty CloseOnClickAwayProperty = DependencyProperty . Register (
337
+ "CloseOnClickAway" , typeof ( bool ) , typeof ( DialogHost ) , new PropertyMetadata ( default ( bool ) ) ) ;
338
+
339
+ /// <summary>
340
+ /// Indicates whether the dialog will close if the user clicks off the dialog, on the obscured background.
341
+ /// </summary>
342
+ public bool CloseOnClickAway
343
+ {
344
+ get { return ( bool ) GetValue ( CloseOnClickAwayProperty ) ; }
345
+ set { SetValue ( CloseOnClickAwayProperty , value ) ; }
346
+ }
347
+
348
+ public static readonly DependencyProperty CloseOnClickAwayParameterProperty = DependencyProperty . Register (
349
+ "CloseOnClickAwayParameter" , typeof ( object ) , typeof ( DialogHost ) , new PropertyMetadata ( default ( object ) ) ) ;
350
+
351
+ /// <summary>
352
+ /// Parameter to provide to close handlers if an close due to click away is instigated.
353
+ /// </summary>
354
+ public object CloseOnClickAwayParameter
355
+ {
356
+ get { return ( object ) GetValue ( CloseOnClickAwayParameterProperty ) ; }
357
+ set { SetValue ( CloseOnClickAwayParameterProperty , value ) ; }
358
+ }
359
+
333
360
public override void OnApplyTemplate ( )
334
361
{
362
+ if ( _contentCoverGrid != null )
363
+ _contentCoverGrid . MouseLeftButtonUp -= ContentCoverGridOnMouseLeftButtonUp ;
364
+
335
365
_popup = GetTemplateChild ( PopupPartName ) as Popup ;
336
366
_popupContentControl = GetTemplateChild ( PopupContentPartName ) as ContentControl ;
367
+ _contentCoverGrid = GetTemplateChild ( ContentCoverGridName ) as Grid ;
368
+
369
+ if ( _contentCoverGrid != null )
370
+ _contentCoverGrid . MouseLeftButtonUp += ContentCoverGridOnMouseLeftButtonUp ;
337
371
338
372
VisualStateManager . GoToState ( this , SelectState ( ) , false ) ;
339
373
@@ -486,6 +520,12 @@ protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
486
520
base . OnPreviewMouseDown ( e ) ;
487
521
}
488
522
523
+ private void ContentCoverGridOnMouseLeftButtonUp ( object sender , MouseButtonEventArgs mouseButtonEventArgs )
524
+ {
525
+ if ( CloseOnClickAway )
526
+ Close ( CloseOnClickAwayParameter ) ;
527
+ }
528
+
489
529
private void OpenDialogHandler ( object sender , ExecutedRoutedEventArgs executedRoutedEventArgs )
490
530
{
491
531
if ( executedRoutedEventArgs . Handled ) return ;
0 commit comments