2024-09-22 11:05:41 +08:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
namespace ShrlAlgoToolkit.Mvvm.Behaviors;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 关闭窗口
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <example><Button Content = "Save" Command="{Binding SaveCommand}"></example>
|
|
|
|
|
|
/// <i:Interaction.Triggers>
|
|
|
|
|
|
/// <i:EventTrigger EventName = "Click" >
|
|
|
|
|
|
/// < i:CallMethodAction MethodName = "CloseTrigger" TargetObject="{Binding RelativeSource={RelativeSource IsMultiSelect=FindAncestor, AncestorType=Windows}}" />
|
|
|
|
|
|
/// </i:EventTrigger >
|
|
|
|
|
|
/// </i:Interaction.Triggers >
|
|
|
|
|
|
///</Button>
|
|
|
|
|
|
|
|
|
|
|
|
public class CloseOnClickBehaviour : Microsoft.Xaml.Behaviors.Behavior<FrameworkElement>
|
|
|
|
|
|
{
|
|
|
|
|
|
public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached(
|
|
|
|
|
|
"IsEnabled",
|
|
|
|
|
|
typeof(bool),
|
|
|
|
|
|
typeof(CloseOnClickBehaviour),
|
|
|
|
|
|
new PropertyMetadata(false, OnIsEnabledChanged)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
public static bool GetIsEnabled(DependencyObject obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (bool)obj.GetValue(IsEnabledProperty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void SetIsEnabled(DependencyObject obj, bool value)
|
|
|
|
|
|
{
|
|
|
|
|
|
obj.SetValue(IsEnabledProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void OnIsEnabledChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs args)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dpo is not Button button)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var oldValue = (bool)args.OldValue;
|
|
|
|
|
|
var newValue = (bool)args.NewValue;
|
|
|
|
|
|
|
|
|
|
|
|
if (!oldValue && newValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
button.Click += OnClick;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (oldValue && !newValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
button.PreviewMouseLeftButtonDown -= OnClick;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void OnClick(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (sender is not Button button)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var win = Window.GetWindow(button);
|
|
|
|
|
|
|
|
|
|
|
|
win?.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|