using Microsoft.Xaml.Behaviors; using System.ComponentModel; using System.Windows; using System.Windows.Input; namespace YwxApp.AiChat.Commands { /// /// Close Window Behavior /// public class ClosingWindowBehavior : Behavior { public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ClosingWindowBehavior), new PropertyMetadata(null)); public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ClosingWindowBehavior), new PropertyMetadata(null)); public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } public object CommandParameter { get { return GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } } protected override void OnAttached() { base.OnAttached(); AssociatedObject.Closing += OnClosing; } protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.Closing -= OnClosing; } private void OnClosing(object sender, CancelEventArgs e) { if (Command != null && Command.CanExecute(CommandParameter)) { Command.Execute(e); } } } }