using System.Windows; using System.Windows.Controls; namespace ShrlAlgoToolkit.RevitAddins.Common.Behavious; /// /// 关闭窗口 /// /// public class CloseOnClickBehaviour : Microsoft.Xaml.Behaviors.Behavior { 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(); } }