chore: dialog can be closed by clicking mask.
This commit is contained in:
1 parent
4178b24758
commit
ca5c292e04
4 files changed
+45
-5
No files matched your search
@@ -8880,10 +8880,11 @@
|
||||
<Setter Property="Focusable" Value="False" />
|
||||
<Setter Property="HorizontalContentAlignment" Value="Center" />
|
||||
<Setter Property="VerticalContentAlignment" Value="Center" />
|
||||
<Setter Property="MaskBrush" Value="{DynamicResource DarkOpacityBrush}" />
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="hc:Dialog">
|
||||
<Border Background="{DynamicResource DarkOpacityBrush}">
|
||||
<Border x:Name="PART_BackElement" Background="{TemplateBinding MaskBrush}">
|
||||
<ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
|
||||
</Border>
|
||||
</ControlTemplate>
|
||||
|
||||
@@ -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<string, FrameworkElement> 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)
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
<Setter Property="Focusable" Value="False"/>
|
||||
<Setter Property="HorizontalContentAlignment" Value="Center"/>
|
||||
<Setter Property="VerticalContentAlignment" Value="Center"/>
|
||||
<Setter Property="MaskBrush" Value="{DynamicResource DarkOpacityBrush}"/>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="hc:Dialog">
|
||||
<Border Background="{DynamicResource DarkOpacityBrush}">
|
||||
<Border x:Name="PART_BackElement" Background="{TemplateBinding MaskBrush}">
|
||||
<ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
|
||||
</Border>
|
||||
</ControlTemplate>
|
||||
@@ -17,4 +18,4 @@
|
||||
</Setter>
|
||||
</Style>
|
||||
|
||||
</ResourceDictionary>
|
||||
</ResourceDictionary>
|
||||
@@ -8997,10 +8997,11 @@
|
||||
<Setter Property="Focusable" Value="False" />
|
||||
<Setter Property="HorizontalContentAlignment" Value="Center" />
|
||||
<Setter Property="VerticalContentAlignment" Value="Center" />
|
||||
<Setter Property="MaskBrush" Value="{DynamicResource DarkOpacityBrush}" />
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="hc:Dialog">
|
||||
<Border Background="{DynamicResource DarkOpacityBrush}">
|
||||
<Border x:Name="PART_BackElement" Background="{TemplateBinding MaskBrush}">
|
||||
<ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
|
||||
</Border>
|
||||
</ControlTemplate>
|
||||
|
||||
Reference in new issue
Block a user