Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call method of the main WPF window from the modal window?

Tags:

c#

wpf

How to call method of the main WPF window from the modal window?

(In fact I want to start some timer of the MainWindow.xaml.cs when I close some model window.)

Thank you!

like image 718
Friend Avatar asked Jan 17 '12 19:01

Friend


1 Answers

What you can do is before opening your second window assign the main window as its owner, then from the modal window call the Owner property, cast it into a MainWindow object and you'll be able to execute the method.

// Code in main window
ModalWindow window = new ModalWindow();
window.Owner = this; 
window.ShowDialog()

//Code on the modal window
var myObject = this.Owner as MainWindow;
myObject.MyMethod(); // Call your method here.
like image 118
Xtian Macedo Avatar answered Oct 23 '22 06:10

Xtian Macedo