简体   繁体   English

更新模态对话框上的父页面是否关闭?

[英]Update parent page on modal dialog close?

I have a window with a grid in it. 我有一个带有网格的窗口。 This window has a button, when this button is pressed, a modal dialog is shown. 该窗口有一个按钮,当按下该按钮时,将显示一个模式对话框。

code: 码:

    private void Edit_Click6S(object sender, RoutedEventArgs e)
    {
        TextEditWindow editWindow = new TextEditWindow();
        editWindow.ShowDialog();
    }

When the user is finished with this dialog, the following code is called: 当用户完成此对话框后,将调用以下代码:

    Window.Close()

But the grid of the parent window is not updated. 但是父窗口的网格不会更新。 Is there a way to reinitialize the parent windows grid when the child dialog is closed? 关闭子对话框时,是否可以重新初始化父窗口网格? Is so, How? 是这样吗?

Since dialogs block execution until they are closed, you can populate the grid directly after your ShowDialog() call: 由于对话框会阻止执行直到关闭对话框,因此您可以在ShowDialog()调用之后直接填充网格:

private void Edit_Click6S(object sender, RoutedEventArgs e)
{
    TextEditWindow editWindow = new TextEditWindow();
    editWindow.ShowDialog();
    PopulateGrid();
}

Otherwise you can handle the dialog's Closed event 否则,您可以处理对话框的Closed事件

editWindow.Closed += dialog_Closed;    

private void dialog_Closed(object sender, System.EventArgs e)
{
    PopulateGrid();
}

To elaborate my comment a little more; 详细阐述我的意见; this is a common question and can be a bit confusing. 这是一个常见的问题,可能会造成混淆。 But use delegates to manage data between the forms. 但是使用委托来管理表单之间的数据。 Here is some code. 这是一些代码。 I can't promise it is ready to compile but it should help show you how to do this correctly. 我不能保证它已准备好进行编译,但它应该有助于向您展示如何正确执行此操作。

 public partial class MainWindow : Window
{
    public delegate void MenuClickedDelegate(); 
    public MainWindow()
    {
       InitializeComponent();
    }
    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
       YourDialog yourDialog = new YourForm();
       yourDialog .MenuClickCallback = new MenuClickedDelegate(this.DoSomething);
       yourDialog .ShowDialog();
    }
    private void DoSomething()
    {
    }
}

Then for your dialog: 然后为您的对话框:

public partial class YourDialog  : Window
{
     public MainWindow.MenuClickedDelegate MenuClickCallback;
     public YourDialog()
     {
         InitializeComponent();
     }

     private void Edit_Click6S(object sender, RoutedEventArgs e)
     {
         this.close();
         MenuClickCallback;
     }
}

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

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