Exposing Inner Control Event in WPF UserControl

// August 16th, 2009 // Programming

When you have controls inside your custom UserControl you can’t call directly to any controls inside user control in WPF. There’s a way to expose them.. First you can add RoutedEvents to a custom control you can declare a public static RoutedEvent in your class and use EventManager.RegisterRoutedEvent() method to register these events. EventManager.RegisterRoutedEvent() has some parameters: the event name, routing strategy, type of RoutedEventHandler and type of the custom control.


Now, create user custom UserControl that contains a button. Here the XAML code :


    
        
    

Than add this code inside .cs file

public partial class CustomUserControl : UserControl
{
    public static readonly RoutedEvent ButtonClickEvent = EventManager.RegisterRoutedEvent(
        "ButtonClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CustomUserControl));

    public event RoutedEventHandler ButtonClick
    {
        add { AddHandler(ButtonClickEvent, value); }
        remove { RemoveHandler(ButtonClickEvent, value); }
    }

    public CustomUserControl()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        RaiseEvent(new RoutedEventArgs(ButtonClickEvent));
    }
}

Here the code where you add that user control, dont forget to add reference :
xmlns:local=”clr-namespace:…. bla2…

So here my Window1 xaml code :


    
        
    

From that listing now your window can recognize property of ButtonClick event. Now here is code in my Window1.xaml. So when button inside user control clicked Message box will appear…

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void CustomUserControl_ButtonClick(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Inner Button Clicked!!");
    }
}

by : Willy Achmat Fauzi

10 Responses to “Exposing Inner Control Event in WPF UserControl”

  1. Brian says:

    Great Article. Thank You.

  2. Robert says:

    Simple, n nice article.. thanks a lot.. :)

  3. same day payday says:

    Amazing blog post, this is very similar to a site that I have.
    Please check it out sometime and feel free to leave me a comenet on it and tell me what you think. Im always looking for feedback.
    http://www.paydaytodayusa.com/

  4. Emily says:

    Simple, n nice article.. thanks a lot.. :)

  5. Amy says:

    Great Article. Thank You.

  6. hey whats your myspace page.

  7. Ignikewrige says:

    very interesting, thanks

  8. Prad says:

    Nice one…

  9. nandana says:

    no event called ‘buttonclick’, this is not working

  10. gorgeouskim says:

    hi there hows it going

Leave a Reply

PHVsPjxsaT48c3Ryb25nPndvb19hYm91dDwvc3Ryb25nPiAtIDwvbGk+PGxpPjxzdHJvbmc+d29vX2Fib3V0bGluazwvc3Ryb25nPiAtICM8L2xpPjxsaT48c3Ryb25nPndvb19hZHNfcm90YXRlPC9zdHJvbmc+IC0gZmFsc2U8L2xpPjxsaT48c3Ryb25nPndvb19hZF9pbWFnZV8xPC9zdHJvbmc+IC0gaHR0cDovL3d3dy53b290aGVtZXMuY29tL2Fkcy93b290aGVtZXMtMTI1eDEyNS0xLmdpZjwvbGk+PGxpPjxzdHJvbmc+d29vX2FkX2ltYWdlXzI8L3N0cm9uZz4gLSBodHRwOi8vd3d3Lndvb3RoZW1lcy5jb20vYWRzL3dvb3RoZW1lcy0xMjV4MTI1LTIuZ2lmPC9saT48bGk+PHN0cm9uZz53b29fYWRfaW1hZ2VfMzwvc3Ryb25nPiAtIGh0dHA6Ly93d3cud29vdGhlbWVzLmNvbS9hZHMvd29vdGhlbWVzLTEyNXgxMjUtMy5naWY8L2xpPjxsaT48c3Ryb25nPndvb19hZF9pbWFnZV80PC9zdHJvbmc+IC0gaHR0cDovL3d3dy53b290aGVtZXMuY29tL2Fkcy93b290aGVtZXMtMTI1eDEyNS00LmdpZjwvbGk+PGxpPjxzdHJvbmc+d29vX2FkX3VybF8xPC9zdHJvbmc+IC0gaHR0cDovL3d3dy53b290aGVtZXMuY29tPC9saT48bGk+PHN0cm9uZz53b29fYWRfdXJsXzI8L3N0cm9uZz4gLSBodHRwOi8vd3d3Lndvb3RoZW1lcy5jb208L2xpPjxsaT48c3Ryb25nPndvb19hZF91cmxfMzwvc3Ryb25nPiAtIGh0dHA6Ly93d3cud29vdGhlbWVzLmNvbTwvbGk+PGxpPjxzdHJvbmc+d29vX2FkX3VybF80PC9zdHJvbmc+IC0gaHR0cDovL3d3dy53b290aGVtZXMuY29tPC9saT48bGk+PHN0cm9u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