我想要一个自定义的关闭窗口。 我的情况是: 原始的X按钮功能是最小化窗口,而自定义实际上将其关闭。我的默认X如下所示:
// add handler by double clicking on Closing event in Properties Box
// My below X button
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
但是,当我尝试创建这样的自定义关闭按钮时:
void closeButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
它将像默认值一样最小化。那我怎么办?感谢您的阅读。
答案 0 :(得分:1)
private bool _forceClose;
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(!_forceClose)
{
e.Cancel = true;
this.Hide();
}
}
void closeButton_Click(object sender, RoutedEventArgs e)
{
_forceClose = true;
Close();
}
答案 1 :(得分:0)
您可以在按钮单击中删除关闭事件
public MainWindow()
{
InitializeComponent();
this.Closing += MainWindow_Closing;
}
void MainWindow_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Closing -= MainWindow_Closing;
this.Close();
}