Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling events of WPF User Control

I have a user control with several buttons, which need to take different actions depending on the class using it.

The problem is that I don't know how to implement those handlers because when using my user control from the final app I don't have direct access to the buttons to specify which handler handles which events.

How would you do that?

like image 740
Gabriel Sanmartin Avatar asked Jun 26 '12 10:06

Gabriel Sanmartin


1 Answers

Another way to do this is to expose the events through events in your UserControl :

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }


    public event RoutedEventHandler Button1Click;

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        if (Button1Click != null) Button1Click(sender, e);     
    }
}

This gives your usercontrol a Button1Click event that hooks up to that button within your control.

like image 178
J... Avatar answered Oct 06 '22 21:10

J...