I have a UserControl, we’ll call it myUC, that is one among several UserControls in the main window (myWindow) of my WPF application. myUC contains a number of standard controls, one of them being a button, we’ll call it myButton.
When I click myButton, I would like to execute myMethod(), which exists in the code-behind of myWindow.
The problem being that myUC doesn’t have any idea that myWindow even exists, much less that myMethod exists.
How can I send the message: ‘Hey, myWindow, wake up. myButton on myUc was just clicked; please run myMethod’?
You can create a command in the window and set the Command property of the button to the name of this command. Clicking the button will fire the command, without a need for a reference to the parent window.
This tutorial explains everything very clearly.