Wpf 将命令绑定到窗口标题栏的X按钮

Wpf 将命令绑定到窗口标题栏的X按钮,wpf,window,command,binding,exit,Wpf,Window,Command,Binding,Exit,我的WPF维护窗口有一个带有“退出”按钮的工具栏;CommandExit与此按钮绑定。CommandExit在退出之前执行一些检查 现在,如果我单击窗口的关闭按钮(标题栏的x按钮),该检查将被忽略 如何将CommandExit绑定到窗口x按钮?我想您可能希望基于这些条件取消关闭?您需要使用,它将向您传递System.ComponentModel.CancelEventArgs以取消关闭 您可以在代码隐藏中钩住此事件并手动执行命令,或者,这是首选方法,您可以使用附加的行为钩住事件并触发命令 大致如

我的WPF维护窗口有一个带有“退出”按钮的工具栏;CommandExit与此按钮绑定。CommandExit在退出之前执行一些检查

现在,如果我单击窗口的关闭按钮(标题栏的x按钮),该检查将被忽略


如何将CommandExit绑定到窗口x按钮?

我想您可能希望基于这些条件取消关闭?您需要使用,它将向您传递System.ComponentModel.CancelEventArgs以取消关闭

您可以在代码隐藏中钩住此事件并手动执行命令,或者,这是首选方法,您可以使用附加的行为钩住事件并触发命令

大致如下(我还没有对此进行测试):

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Interactive;
使用System.Windows.Input;
命名空间行为
{
公共类WindowCloseBehavior:行为
{
/// 
///要执行的命令
/// 
public static readonly dependencProperty CommandProperty=dependencProperty.Register(“命令”、typeof(ICommand)、typeof(WindowCloseBehavior)、new UIPropertyMetadata(null));
/// 
///获取或设置命令
/// 
公共ICommand命令
{
得到
{
返回(ICommand)this.GetValue(CommandProperty);
}
设置
{
this.SetValue(CommandProperty,value);
}
}
受保护的覆盖无效附加()
{
base.onatached();
this.AssociatedObject.Closing+=OnWindowClosing;
}
void OnWindowClosing(对象发送方,System.ComponentModel.CancelEventArgs e)
{
if(this.Command==null)
返回;
//根据您的工作方式(以及是否显示确认对话框等),您可能只需要执行以下操作:
//e.Cancel=!this.Command.CanExecute();
//如果命令的CanExecute返回false,这将取消窗口关闭。
//
//或者,您可以检查它是否可以执行,并让命令本身执行
//改变,取消
如果(!this.Command.CanExecute(e))
返回;
这个.Command.Execute(e);
}
附加时受保护的覆盖无效()
{
base.OnDetaching();
this.AssociatedObject.Closing-=OnWindowClosing;
}
}
}

我想您可能想根据这些条件取消结账?您需要使用,它将向您传递System.ComponentModel.CancelEventArgs以取消关闭

您可以在代码隐藏中钩住此事件并手动执行命令,或者,这是首选方法,您可以使用附加的行为钩住事件并触发命令

大致如下(我还没有对此进行测试):

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Interactive;
使用System.Windows.Input;
命名空间行为
{
公共类WindowCloseBehavior:行为
{
/// 
///要执行的命令
/// 
public static readonly dependencProperty CommandProperty=dependencProperty.Register(“命令”、typeof(ICommand)、typeof(WindowCloseBehavior)、new UIPropertyMetadata(null));
/// 
///获取或设置命令
/// 
公共ICommand命令
{
得到
{
返回(ICommand)this.GetValue(CommandProperty);
}
设置
{
this.SetValue(CommandProperty,value);
}
}
受保护的覆盖无效附加()
{
base.onatached();
this.AssociatedObject.Closing+=OnWindowClosing;
}
void OnWindowClosing(对象发送方,System.ComponentModel.CancelEventArgs e)
{
if(this.Command==null)
返回;
//根据您的工作方式(以及是否显示确认对话框等),您可能只需要执行以下操作:
//e.Cancel=!this.Command.CanExecute();
//如果命令的CanExecute返回false,这将取消窗口关闭。
//
//或者,您可以检查它是否可以执行,并让命令本身执行
//改变,取消
如果(!this.Command.CanExecute(e))
返回;
这个.Command.Execute(e);
}
附加时受保护的覆盖无效()
{
base.OnDetaching();
this.AssociatedObject.Closing-=OnWindowClosing;
}
}
}

您必须实现主窗口事件“关闭”的事件处理程序,您可以在其中执行检查并取消关闭操作。这是最简单的方法,但是,否则您必须重新设计整个窗口及其主题。

您必须实现主窗口事件“关闭”的事件处理程序,您可以在其中执行检查并取消关闭操作。这是最简单的方法,但是,否则您必须重新设计整个窗口及其主题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Input;

namespace Behaviors
{
    public class WindowCloseBehavior : Behavior<Window>
    {
        /// <summary>
        /// Command to be executed
        /// </summary>
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(WindowCloseBehavior), new UIPropertyMetadata(null));

        /// <summary>
        /// Gets or sets the command
        /// </summary>
        public ICommand Command
        {
            get
            {
                return (ICommand)this.GetValue(CommandProperty);
            }

            set
            {
                this.SetValue(CommandProperty, value);
            }
        }

        protected override void OnAttached()
        {
            base.OnAttached();

            this.AssociatedObject.Closing += OnWindowClosing;
        }

        void OnWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.Command == null)
                return;

            // Depending on how you want to work it (and whether you want to show confirmation dialogs etc) you may want to just do:
            // e.Cancel = !this.Command.CanExecute();
            // This will cancel the window close if the command's CanExecute returns false.
            //
            // Alternatively you can check it can be excuted, and let the command execution itself
            // change e.Cancel

            if (!this.Command.CanExecute(e))
                return;

            this.Command.Execute(e);
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            this.AssociatedObject.Closing -= OnWindowClosing;
        }

    }
}