I can't get my custom click event to work.

A N 81 Reputation points
2020-11-25T02:23:20.583+00:00

Hello. I have this class written.

public class MyButtonSimple : 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(MyButtonSimple));

    // 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);
        RaiseEvent(newEventArgs);
    }
    // For demonstration purposes we raise the event when the MyButtonSimple is clicked
    protected override void OnClick()
    {
        RaiseTapEvent();
    }

It works fine, I click it and it shows a MessageBox.
The thing is that I want to change the class it derives from to "UserControl".
And I do not want it to derive from Button. Though, it doesn't work when I
change it and try to click it, nothing happens.

Please help!

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,667 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points
    2020-11-25T05:09:42.32+00:00

    The OnClick is not belonged to UserControl Methods, you can override any UserControl Methods on your needs. In my test, I override the Method OnMouseLeftButtonDown to raise the RaiseTapEvent for UserControl, below is my edited MyButtonSimple.cs

     public class MyButtonSimple:UserControl  
        {  
            // 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(MyButtonSimple));  
      
            // 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);  
                RaiseEvent(newEventArgs);  
            }  
            // For demonstration purposes we raise the event when the MyButtonSimple is clicked  
      
            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)  
            {  
                RaiseTapEvent();  
            }  
        }  
    

    To use it in MainWindow.xaml:

      <local:MyButtonSimple Width="180" Height="30" Background="Aqua" Tap="MyButtonSample_Tab" ></local:MyButtonSimple>  
    

    The MainWindow.xaml.cs code is:

     private void MyButtonSample_Tab(object sender, RoutedEventArgs e)  
            {  
                MessageBox.Show("This is MyButtonSimple:UserControl.");  
            }  
    

    When I clicked the MyButtonSimple, the result picture is:
    42760-1.png

    This is my understanding for youe question, if I misunderstand, please point out and descibe your question in details which will beneficial for me to analyze.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful