@@ -36,12 +36,14 @@ public enum DialogHostOpenDialogCommandDataContextSource
3636
3737 [ TemplatePart ( Name = PopupPartName , Type = typeof ( Popup ) ) ]
3838 [ TemplatePart ( Name = PopupPartName , Type = typeof ( ContentControl ) ) ]
39+ [ TemplatePart ( Name = ContentCoverGridName , Type = typeof ( Grid ) ) ]
3940 [ TemplateVisualState ( GroupName = "PopupStates" , Name = OpenStateName ) ]
4041 [ TemplateVisualState ( GroupName = "PopupStates" , Name = ClosedStateName ) ]
4142 public class DialogHost : ContentControl
4243 {
4344 public const string PopupPartName = "PART_Popup" ;
4445 public const string PopupContentPartName = "PART_PopupContentElement" ;
46+ public const string ContentCoverGridName = "PART_ContentCoverGrid" ;
4547 public const string OpenStateName = "Open" ;
4648 public const string ClosedStateName = "Closed" ;
4749
@@ -62,6 +64,7 @@ public class DialogHost : ContentControl
6264
6365 private Popup _popup ;
6466 private ContentControl _popupContentControl ;
67+ private Grid _contentCoverGrid ;
6568 private DialogSession _session ;
6669 private DialogOpenedEventHandler _attachedDialogOpenedEventHandler ;
6770 private DialogClosingEventHandler _attachedDialogClosingEventHandler ;
@@ -330,10 +333,41 @@ public DialogHostOpenDialogCommandDataContextSource OpenDialogCommandDataContext
330333 set { SetValue ( OpenDialogCommandDataContextSourceProperty , value ) ; }
331334 }
332335
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+
333360 public override void OnApplyTemplate ( )
334361 {
362+ if ( _contentCoverGrid != null )
363+ _contentCoverGrid . MouseLeftButtonUp -= ContentCoverGridOnMouseLeftButtonUp ;
364+
335365 _popup = GetTemplateChild ( PopupPartName ) as Popup ;
336366 _popupContentControl = GetTemplateChild ( PopupContentPartName ) as ContentControl ;
367+ _contentCoverGrid = GetTemplateChild ( ContentCoverGridName ) as Grid ;
368+
369+ if ( _contentCoverGrid != null )
370+ _contentCoverGrid . MouseLeftButtonUp += ContentCoverGridOnMouseLeftButtonUp ;
337371
338372 VisualStateManager . GoToState ( this , SelectState ( ) , false ) ;
339373
@@ -486,6 +520,12 @@ protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
486520 base . OnPreviewMouseDown ( e ) ;
487521 }
488522
523+ private void ContentCoverGridOnMouseLeftButtonUp ( object sender , MouseButtonEventArgs mouseButtonEventArgs )
524+ {
525+ if ( CloseOnClickAway )
526+ Close ( CloseOnClickAwayParameter ) ;
527+ }
528+
489529 private void OpenDialogHandler ( object sender , ExecutedRoutedEventArgs executedRoutedEventArgs )
490530 {
491531 if ( executedRoutedEventArgs . Handled ) return ;
0 commit comments