简体   繁体   English

如何设置对话框的所有者窗口

[英]How to set the owner window of a dialog

I've got a WPF application with these two separated things: 我有一个WPF应用程序,其中包含两个独立的部分:

  • Main Window 主视窗
  • Dialog box (prompt out when the close button on the main window is clicked, if [yes] is clicked, whole application will close, if [no] is clicked, back to the main window) 对话框(单击主窗口上的关闭按钮时提示,如果单击[是],将关闭整个应用程序;如果单击[否],则返回到主窗口)

Here's my code when the close button on main window is clicked: 这是单击主窗口上的关闭按钮时的代码:

private void CloseBtn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
                SPMessageBox msgBox = new SPMessageBox();
                msgBox.Owner = this;
                msgBox.ShowDialog();          
        }

And here's the code when the [yes] and [no] button on the dialog box are clicked: 这是单击对话框上的[是]和[否]按钮时的代码:

public void Yes_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

private void No_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

When the [no] button is clicked, the whole application stops working. 单击[否]按钮时,整个应用程序停止工作。 Is it because the owner is changed so it can't get back to the main window? 是否因为所有者已更改而无法返回主窗口? How to handle this problem so that after the [no] button is clicked it can go back to the main window? 如何处理此问题,以便在单击[no]按钮后可以返回主窗口?

Not sure about the owner issue or what happens when you call this.Close(), but this is how I've learned to handle dialog clicks: 不确定所有者问题或调用this.Close()时会发生什么,但这是我学会处理对话框单击的方式:

private void Yes_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;
}
private void No_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = false;
}

Provided that SPMessageBox inherits from Window this should close the dialog and let you handle the result in CloseBtn_MouseLeftButtonDown like this: 假设SPMessageBox从Window继承,则应关闭对话框,并让您在CloseBtn_MouseLeftButtonDown中处理结果,如下所示:

if (msgBox.ShowDialog() == true)
   Application.Current.Shutdown();

The "== true" is necessary since the return type is bool? 因为返回类型为bool,所以必须使用“ == true”? (nullable bool). (null布尔)。

More info under "Setting the Modal Dialog Result" a bit down on this MSDN page 此MSDN页面上的“设置模态对话框结果”下的更多信息

As said in my comment, I prefer the Closing event of the window to cancel it if necessary. 如我的评论所述,我希望在需要时取消窗口的Closing事件。

In this case it is important that SPMessageBox returns a value that determine the user choice of closing the Application. 在这种情况下,重要的是SPMessageBox返回一个值,该值确定用户选择关闭应用程序。 It can be done with the DialogResult property: 可以使用DialogResult属性来完成:

public void Yes_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;//close application
    this.Close();
}

private void No_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = false;//keep application open
    this.Close();
}

And here is the calling of the dialog: 这是对话框的调用:

private void Window_Closing(object sender, CancelEventArgs e)
{
    SPMessageBox msgBox = new SPMessageBox();
    msgBox.Owner = this;
    if (msgBox.ShowDialog() == false)
    {
        e.Cancel = true;//cancel closing the window
    }
}

But I am not sure if this is solves the real problem. 但是我不确定这是否解决了真正的问题。 I tested also your code in a new WPF application and got no problems with it. 我还在新的WPF应用程序中测试了您的代码,但没有遇到任何问题。 I think there must be another part in your code, that makes problems. 我认为您的代码中肯定还有另一部分会引起问题。 The assignment of Owner should not be problematic. Owner的分配应该没有问题。

You should set the .DialogResult property to properly close a dialog. 您应该设置.DialogResult属性以正确关闭对话框。 The code that opens the dialog should check the result and actually perform the operation. 打开对话框的代码应检查结果并实际执行操作。

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

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