【发布时间】:2012-01-11 04:47:57
【问题描述】:
我意识到当我设置退出按钮属性为 IsCancel = True 时,窗口的 Closing 事件会触发两次。
private void exitButton_Click(object sender, RoutedEventArgs e)
{
// this button was set attribute IsCancel = True.
Close();
}
private void BaseWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("test"); // this message box will show twice
// when you click on the exit button
e.Cancel = true;
}
这是 WPF 的错误吗?有解决办法吗?
Ps:对不起,我忘了说这个错误只发生在你从父窗口调用你的窗口时。
【问题讨论】:
-
如果您删除
e.Cancel = true导致窗口实际关闭会怎样? -
是的,如果我删除 e.Cancel = true,窗口将关闭。
-
但是我希望窗口取消关闭所以我需要设置 e.Cancel = true