How to call method of a parent window when another daughter window is closed with WPF

1

Hello, I'm starting to work with WPF and my situation is as follows:

  • I have a MainWindow screen, which persists the application.
  • MainWindow calls a new RuleDetailsDialog screen on a given function.
  • As soon as a process that closes the RuleDetailsDialog screen is executed I need to update the MainWindow.

In MainWindow, the method that calls theDetailsDialog Rule is below:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    private void AddRegra(object sender, RoutedEventArgs e)
    {
        RegraDetailsDialog rdd = new RegraDetailsDialog();
        rdd.Show();
    }
    private void AtualizaTela(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("teste");
    }
}

How do I, once the DetailsDialog Rule is closed I can call the AtualizaTela method on MainWindow?

    
asked by anonymous 11.07.2018 / 13:58

2 answers

1

If you call the ShowDialog () opens a window and returns only when the newly opened window is closed , and of course call soon after the method, example:

private void AddRegra(object sender, RoutedEventArgs e)
{
    RegraDetailsDialog rdd = new RegraDetailsDialog();
    rdd.ShowDialog(); 
    AtualizaTela();
}
private void AtualizaTela()
{
    MessageBox.Show("teste");
}

will only execute AtualizaTela when rdd is closed.

Reference:

11.07.2018 / 14:22
0

In your XAML of the RuleDetailsDialog window you can put

<Window...Closing="Window_Closing" Closed="Window_Closed">

</Window>

In the class place the code below to call methods of the class MainWindow.

private void Window_Closed(object sender, EventArgs e)
{
     (Application.Current.MainWindow as MainWindow).AtualizaTela();
}
    
11.07.2018 / 14:26