我在WPF中创建了一个自定义对话框。我想,当用户点击X时,对话框将被打开,如果他想要退出,请点击“是”,否则他应该点击“否”。
static public bool? close;
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.Hide();
WindowClose windowClose = new WindowClose();
windowClose.ShowDialog();
close = windowClose.DialogResult;
if (close == true)
{
Application.Current.Shutdown();
}
else
{
//DONT CLOSE THE APPLICATION AND SHOW THE CURRENT WINDOW
}
}
对话框的代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
this.Close();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
this.Close();
}
答案 0 :(得分:0)
不确定它在WPF中的工作方式是否有所不同,但在对话框代码中的标准winforms中,您将设置(例如)DialogResult到DialogResult.OK(是)和DialogResult.Cancel(否)。您不需要关闭表单,因为在设置DialogResult
时会自动完成然后在主要表单中你有
If (windowClose.ShowDialog() == DialogResult.OK
{
Application.Current.Shutdown();
}
else
{
//DONT CLOSE THE APPLICATION AND SHOW THE CURRENT WINDOW
}