s

wpf how to call a function of the main window from another window edit button Edit

author
Pooran Prasad Rajanna | calendar 09 September 2020 | 4987

1) Create the main window as a singleton class, then you can reach it by simply requesting it's instance (and I assume there is only one "main window").

2) Create an interface like this:

public interface IMainWindow
{
    void RefreshCustomers();
}

Then, implement the interface in the main window and set the owner of the child window to main window (assuming you run the code below in main window):

MyChildWindow childWindow = new MyChildWindow();
childWindow.Owner = this;
Finally, in your child window, you can use this:

IMainWindow mainWindow = Owner as IMainWindow;
if (mainWindow != null)
{
     mainWindow.RefreshCustomers();
}