简体   繁体   English

使用“关闭”(x)按钮关闭窗口如果我已经触发了“关闭事件”?

[英]Closing the window with Close(x) button If I already fired Closing Event?

我已经在窗口上设置了关闭事件,如果我的验证失败,则会显示消息,但是我想关闭顶部带有X按钮的窗口,而他做了同样的事情,向我展示了如何避免这种情况并在X按钮上关闭窗口,我可以吗?使用此按钮绕过关闭事件我不在做MVVM应用程序吗?

You can indeed use the Closing event to hook up some validation. 您确实可以使用Closing事件来进行一些验证。 This event fires whenever the window is about to close, whether by calling Close() , using the "X" close button or hitting Alt + F4 . 每当窗口即将关闭时,无论是通过调用Close() ,使用“ X”关闭按钮还是按下Alt + F4键,都会触发此事件。 This event can also be cancelled which would leave the window open. 也可以取消此事件,这将使窗口保持打开状态。

For example: 例如:

class MyWindow : Window
{
    protected override void OnClosing(CancelEventArgs e)
    {
        base.OnClosing(e);

        e.Cancel = !IsValid(); // your validation code
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM