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

Asked

Viewed 184 times

1

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

  • I have a Mainwindow screen, which persists the application.
  • The Mainwindow calls a new screen Regradetailsdialog in a certain function.
  • Once a process is executed that closes the screen Rewrite SDIALOG need to update the Mainwindow.

In Mainwindow, the method that calls Regradetailsdialog 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, as soon as the Regradetailsdialog is closed, call the method AtualizaTela in Mainwindow?

  • You need to call a method from another form... Correct ? Note my answer.

2 answers

1


  • So I just need to change the .Show() for .ShowDialog()?

  • 1

    @Leandroluk yes... I would do it, for being more logical, when I abacar to use the screen daughter update the screen father.

  • 1

    It really solves my problem... I honestly didn’t think it would be such a simple thing... thank you very much.

0

In your XAML of the window Regradetailsdialog you can put

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

</Window>

In the class put the code below to call Mainwindow class methods.

private void Window_Closed(object sender, EventArgs e)
{
     (Application.Current.MainWindow as MainWindow).AtualizaTela();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.