Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an application in wpf where I use UserControl to fill some data.I need some actions performed in the parent window while clicking on a button in UserControl.
I tried using events and delegates but failed to call the function in parent window.

What I have tried:

in my usercontrol I have code like this
// A delegate type for hooking up change notifications.
    public delegate void Button_ClickedEventHandler(object sender, EventArgs e);

..........
.........
// An event that clients can use to be notified whenever the
       // the button is clicked
       public event Button_ClickedEventHandler ButtonClicked;

................
................

// Invoke the event; called whenever button clicked
public void button1_Click(object sender, RoutedEventArgs e)
       {

              if (this.ButtonClicked != null)
               this.ButtonClicked(this,e);
       }

..........................................
and in the parent window
public myWindow()
       {
           InitializeComponent();
           myClass1.ButtonClicked += new Button_ClickedEventHandler(myClass1_ButtonClicked);

       }

void myClass1_ButtonClicked(object sender, EventArgs e)
       {
           throw new NotImplementedException();
       }
Posted
Updated 17-Jun-17 19:51pm

1 solution

Just add an event and then get the container form to handle it: A Simple Code Snippet to Add an Event[^] - it shows the code you need, and how to "automate it" in VS.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900