Skip to content

Commit 3ad0a85

Browse files
committed
ability to close a dialog by clicking away. fixes #238
1 parent 2d6167b commit 3ad0a85

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

MainDemo.Wpf/Dialogs.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@
147147
Grid.Column="3" Margin="8 0 8 0"
148148
>SAMPLE 4: Dialog managed from view model using IsOpen and custom commands (ignoring the provided routed commands).</TextBlock>
149149
<materialDesign:DialogHost Grid.Column="3" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"
150-
IsOpen="{Binding IsSample4DialogOpen}"
151-
DialogContent="{Binding Sample4Content}">
150+
IsOpen="{Binding IsSample4DialogOpen}"
151+
DialogContent="{Binding Sample4Content}"
152+
CloseOnClickAway="True">
152153
<Border BorderThickness="1" BorderBrush="{DynamicResource PrimaryHueMidBrush}"
153154
MinWidth="256" MinHeight="256" ClipToBounds="True">
154155
<Button HorizontalAlignment="Center" VerticalAlignment="Center"

MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

MaterialDesignThemes.Wpf/Themes/Generic.xaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@
478478
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="PART_Popup" Storyboard.TargetProperty="IsOpen">
479479
<DiscreteBooleanKeyFrame Value="True" KeyTime="0" />
480480
</BooleanAnimationUsingKeyFrames>
481-
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ContentGrid" Storyboard.TargetProperty="Opacity">
481+
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_ContentCoverGrid" Storyboard.TargetProperty="Opacity">
482482
<EasingDoubleKeyFrame Value="0" KeyTime="0" />
483483
<EasingDoubleKeyFrame Value="0.56" KeyTime="0:0:0.3">
484484
<EasingDoubleKeyFrame.EasingFunction>
@@ -518,7 +518,7 @@
518518
<DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0" />
519519
<DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0.3" />
520520
</BooleanAnimationUsingKeyFrames>
521-
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ContentGrid" Storyboard.TargetProperty="Opacity">
521+
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_ContentCoverGrid" Storyboard.TargetProperty="Opacity">
522522
<EasingDoubleKeyFrame Value="0.56" KeyTime="0" />
523523
<EasingDoubleKeyFrame Value="0" KeyTime="0:0:0.3">
524524
<EasingDoubleKeyFrame.EasingFunction>
@@ -561,7 +561,7 @@
561561
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="PART_Popup" Storyboard.TargetProperty="IsOpen">
562562
<DiscreteBooleanKeyFrame Value="True" KeyTime="0" />
563563
</BooleanAnimationUsingKeyFrames>
564-
<DoubleAnimation Storyboard.TargetName="ContentGrid" Storyboard.TargetProperty="Opacity"
564+
<DoubleAnimation Storyboard.TargetName="PART_ContentCoverGrid" Storyboard.TargetProperty="Opacity"
565565
Duration="0"
566566
To=".56" />
567567
<DoubleAnimation Storyboard.TargetName="PART_PopupContentElement" Storyboard.TargetProperty="Opacity"
@@ -631,11 +631,12 @@
631631
x:Name="ContentPresenter" Opacity="1"
632632
Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" />
633633
</AdornerDecorator>
634-
<Grid x:Name="ContentGrid" Background="Black" Opacity="0" IsHitTestVisible="False" Focusable="False" />
634+
<Grid x:Name="PART_ContentCoverGrid" Background="Black" Opacity="0" IsHitTestVisible="False" Focusable="False" />
635635
</Grid>
636636
<ControlTemplate.Triggers>
637637
<Trigger Property="IsOpen" Value="True">
638638
<Setter TargetName="ContentPresenter" Property="IsEnabled" Value="False" />
639+
<Setter TargetName="PART_ContentCoverGrid" Property="IsHitTestVisible" Value="True" />
639640
</Trigger>
640641
</ControlTemplate.Triggers>
641642
</ControlTemplate>

0 commit comments

Comments
 (0)