Get the MainWindow in the Current Application

By Michael Detras

We may have a WPF application containing many Window objects. We may want to access a method of the MainWindow from another Window.

We can get the current Application using the Application class' static Current property. We can then get the MainWindow via the Application's MainWindow property.

MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;
mainWindow.SomeMethod();

Get the MainWindow in the Current Application  (1698 Views)