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

发布于 2024-09-02 21:17:19 字数 158 浏览 1 评论 0原文

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

现在,如果我单击窗口的关闭按钮(标题栏的 x 按钮),则会忽略此检查。

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

My WPF maintenance window have a toolbar with a button "Exit"; a CommandExit is bind with this button. The CommandExit perform some checks before exit.

Now, if I click to close button of the window (x-button of title bar), this checks are ignored.

How can I do to bind CommandExit to window x-button?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几度春秋 2024-09-09 21:17:19

我想您可能想根据这些条件取消关闭?您需要使用关闭事件,它会向您传递一个 System.ComponentModel.CancelEventArgs 来取消关闭。

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

类似的东西(我还没有测试过):

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;
        }

    }
}

I assume you may want to cancel closing based on these conditions? You need to use the Closing event, which passes you a System.ComponentModel.CancelEventArgs to cancel the close.

You can either hook this event in code-behind and execute the command manually, or, and this would be the preferred approach, you could use an attached behavior to hook the event and fire the command.

Something along the lines of (and I haven't tested this) :

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;
        }

    }
}
野鹿林 2024-09-09 21:17:19

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

You have to implement event handler of your main window's event "Closing" where you can do checks and cancel the closing action. This is easiest way to do it, however otherwise you have to redesign whole window and its theme.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文