WPF call method parent from usercontrol WPF call method parent from usercontrol wpf wpf

WPF call method parent from usercontrol


You'll need to cast the Window object to the specific window type you're using - which in your case is MainWindow:

MainWindow win = (MainWindow)Window.GetWindow(this);win.getList();

However, it's not wise to have such coupling between the user control and the window it's hosted in, since that means you will only be able to use it in a window of type MainWindow. It would be better to expose a dependency property in the user control and bind the list to that property - this way the user control will have the data it requires and it will also be reusable in any type of window.


Solution from @Adi Lester is working but it break WPF coding ruler. Proper way of doing this is using event like in the linked answer belowhttps://stackoverflow.com/a/19384953/3099317