How to add click event handler to Custom User Control RRS feed

  • Question

  • Hello. I want to know how I can add an event such as Button.Click to my custom control so that

    I can make something happen when it is clicked.

    I'm rather new to C# and WPF programming so I hope I don't sound too dumb..

    Any help is appreciated. (Request clarification if needed.)

    I am making a TestButton class to see how hard events are to handle.

    Can someone tell me how to add a .Click event?

    Tuesday, November 24, 2020 6:03 PM

All replies

  • Hello,

    See the following where the RoutedEventHandler is specified as public


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Check out:  the new Microsoft Q&A forums

    Tuesday, November 24, 2020 9:02 PM
  • This didn't really help. At least the stackoverflow link didn't...

    It showed how to add a button with a click event I think.

    I want to know how to add a click event, or other event, (like TextChanged)

    to my actual UserControl.

    Tuesday, November 24, 2020 9:31 PM
  • So, I got this code to work:

    public class MyButton : Button
            // Create a custom routed event by first registering a RoutedEventID
            // This event uses the bubbling routing strategy
            public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
                "Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButton));
            // Provide CLR accessors for the event
            public event RoutedEventHandler Tap
                add { AddHandler(TapEvent, value); }
                remove { RemoveHandler(TapEvent, value); }
            // This method raises the Tap event
            void RaiseTapEvent()
                RoutedEventArgs newEventArgs = new RoutedEventArgs(MyButtonSimple.TapEvent);
            // For demonstration purposes we raise the event when the MyButton is clicked
            protected override void OnClick()

    But when I change the "MyButton : Button" to "MyButton : UserControl", run the program and click the object, it does nothing.

    Wednesday, November 25, 2020 2:13 AM
  • Hi Alexander125,
    For questions about WPF, I suggest you ask the question on the Microsoft Q&A forum and you can get more professional answer.
    Thank you for your understanding.
    Best Regards,
    Daniel Zhang

    "Visual c#" forum will be migrating to a new home on Microsoft Q&A ! We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A ! For more information, please refer to the sticky post.

    Wednesday, November 25, 2020 8:46 AM
  • Thank you.

    Wednesday, November 25, 2020 6:01 PM