The Best C# Programmer In The World - Ben Perkins Member Login  | Newsletter | RSS Feeds 
The Best C# Programmer In The World - Ben Perkins
Call a parent method from UserControl using WPF in C#  
Call a parent method from UserControl using WPF in C#
I was implemting some paging functionality in a system I was building and realized that I had created the same forward, backward, first and last buttons and logic 3 times. After doing this I realized these buttons and logic were a perfect example of where I could reduce complexity and encapsulate my code better. I wasn't able to easily find an example for a situation I ran in to. I needed to call a method from the user control that existed on the parent WPF window. After some trial and error I came up with the below solution.
In the User Control place the below logic.
                        public event EventHandler ExecuteMethod;

                        protected virtual void OnExecuteMethod()
                            if (ExecuteMethod != null) ExecuteMethod(this, EventArgs.Empty);

                        public void ChildButton_Click(object sender, EventArgs e)
I first create an event and then a virtual method to call the event. As you can see, when you want to execute an event, you need to pass it the (object, sender) standard parameters. FInally, I had a button in the control, that when pressed would execute the method that in turn executes the event.
In the Parent WPF MainWindow, I added this code:
                        <Child:className Name="uc_Child" />
Within the opening Window tag, you need to place the reference to your User Control. As well, add it to the references for the project. Then add the tag to your WPF window where you want it displayed.
In the Parent WPF code-behind, MainWindow.xaml.cs, I added this code:
                        protected void ParentWPF_method(object sender, EventArgs e)
                           MessageBox.Show("ParentWPF_method called by ChildButton_Click", 
                                           "User Control Test", MessageBoxButton.OK, 

                        public MainWindow()

                           uc_Child.ExecuteMethod += new EventHandler(ParentWPF_method);
The ParentWPF_method is the method I want called / executed when the ChildButton_Click occurs. For this example, it simply deploys a messagebox saying that the method was successfully called. In the constructor of the MainWIndow I "wire up" the User Controls' ExecuteMethod event to the ParentWPF_method function. That is where the magic happens...
I saw a number of other examples that seemed much more complex and others that tightly coupled the user control and parent together. Both things I wanted to avoid when implementing my solution.
Feedback / Question
Your Name:Your Email:
I had to remove the capability to leave feedback due to this. Will be back soon.
  Posts: 123
  Comments: 87
  Fundamentals: 16


2014 September (11)
2014 February (5)
2014 January (5)
2013 December (2)
2013 November (2)
2012 December (2)
2012 November (2)
2012 October (2)
2012 September (2)
2012 August (1)
2012 July (3)
2012 June (2)
2012 May (4)
2012 April (5)
2012 March (4)
2012 February (4)
2012 January (5)
2011 December (2)
2011 November (6)
2011 October (7)
2011 September (7)
2011 August (9)
2011 July (9)
2011 June (8)
2011 May (9)
2011 April (7)
2011 March (9)
2011 February (8)
2011 January (8)
2010 December (7)
2010 November (8)
2010 October (4)
free counters
The sample code on this website is provided to illustrate a concept and should not be used in applications or Web sites without proper professional consultation, as it may not illustrate the safest coding practices. I assume no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

The Best C# Programmer In The World - Ben Perkins, © 2010, All Rights ReservedContact Ben