From ca5c292e041f94227dabbe159f99092370118d74 Mon Sep 17 00:00:00 2001
From: NaBian <836904362@qq.com>
Date: Tue, 8 Nov 2022 22:24:57 +0800
Subject: [PATCH] chore: dialog can be closed by clicking mask.
---
.../HandyControl_Net_40/Themes/Theme.xaml | 3 +-
.../Controls/Dialog/Dialog.cs | 39 ++++++++++++++++++-
.../Themes/Styles/Base/DialogBaseStyle.xaml | 5 ++-
.../HandyControl_Shared/Themes/Theme.xaml | 3 +-
4 files changed, 45 insertions(+), 5 deletions(-)
diff --git a/src/Net_40/HandyControl_Net_40/Themes/Theme.xaml b/src/Net_40/HandyControl_Net_40/Themes/Theme.xaml
index 52766276..67cbd792 100644
--- a/src/Net_40/HandyControl_Net_40/Themes/Theme.xaml
+++ b/src/Net_40/HandyControl_Net_40/Themes/Theme.xaml
@@ -8880,10 +8880,11 @@
+
-
+
diff --git a/src/Shared/HandyControl_Shared/Controls/Dialog/Dialog.cs b/src/Shared/HandyControl_Shared/Controls/Dialog/Dialog.cs
index eb527cfd..803053ca 100644
--- a/src/Shared/HandyControl_Shared/Controls/Dialog/Dialog.cs
+++ b/src/Shared/HandyControl_Shared/Controls/Dialog/Dialog.cs
@@ -4,16 +4,20 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
+using System.Windows.Media;
using HandyControl.Data;
using HandyControl.Interactivity;
using HandyControl.Tools;
namespace HandyControl.Controls;
+[TemplatePart(Name = BackElement, Type = typeof(Border))]
public class Dialog : ContentControl
{
- private string _token;
+ private const string BackElement = "PART_BackElement";
+ private string _token;
+ private Border _backElement;
private AdornerContainer _container;
private static readonly Dictionary ContainerDic = new();
@@ -27,6 +31,24 @@ public class Dialog : ContentControl
internal set => SetValue(IsClosedProperty, ValueBoxes.BooleanBox(value));
}
+ public static readonly DependencyProperty MaskCanCloseProperty = DependencyProperty.RegisterAttached(
+ "MaskCanClose", typeof(bool), typeof(Dialog), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits));
+
+ public static void SetMaskCanClose(DependencyObject element, bool value)
+ => element.SetValue(MaskCanCloseProperty, ValueBoxes.BooleanBox(value));
+
+ public static bool GetMaskCanClose(DependencyObject element)
+ => (bool) element.GetValue(MaskCanCloseProperty);
+
+ public static readonly DependencyProperty MaskBrushProperty = DependencyProperty.Register(
+ nameof(MaskBrush), typeof(Brush), typeof(Dialog), new PropertyMetadata(default(Brush)));
+
+ public Brush MaskBrush
+ {
+ get => (Brush) GetValue(MaskBrushProperty);
+ set => SetValue(MaskBrushProperty, value);
+ }
+
public static readonly DependencyProperty TokenProperty = DependencyProperty.RegisterAttached(
"Token", typeof(string), typeof(Dialog), new PropertyMetadata(default(string), OnTokenChanged));
@@ -155,6 +177,21 @@ public class Dialog : ContentControl
}
}
+ public override void OnApplyTemplate()
+ {
+ base.OnApplyTemplate();
+
+ _backElement = GetTemplateChild(BackElement) as Border;
+ }
+
+ protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
+ {
+ if (GetMaskCanClose(this) && _backElement is { IsMouseDirectlyOver: true })
+ {
+ Close();
+ }
+ }
+
private void Close(DependencyObject element)
{
if (element != null && _container != null)
diff --git a/src/Shared/HandyControl_Shared/Themes/Styles/Base/DialogBaseStyle.xaml b/src/Shared/HandyControl_Shared/Themes/Styles/Base/DialogBaseStyle.xaml
index e00ac432..1323c212 100644
--- a/src/Shared/HandyControl_Shared/Themes/Styles/Base/DialogBaseStyle.xaml
+++ b/src/Shared/HandyControl_Shared/Themes/Styles/Base/DialogBaseStyle.xaml
@@ -6,10 +6,11 @@
+
-
+
@@ -17,4 +18,4 @@
-
\ No newline at end of file
+
diff --git a/src/Shared/HandyControl_Shared/Themes/Theme.xaml b/src/Shared/HandyControl_Shared/Themes/Theme.xaml
index fe60ec51..9893456d 100644
--- a/src/Shared/HandyControl_Shared/Themes/Theme.xaml
+++ b/src/Shared/HandyControl_Shared/Themes/Theme.xaml
@@ -8997,10 +8997,11 @@
+
-
+