question

AN-5534 avatar image
0 Votes"
AN-5534 asked AN-5534 edited

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

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!


dotnet-csharpwindows-wpfdotnet-wpf-xaml
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


What do you click: the User Control or the button, which is added to User Control?

0 Votes 0 ·

There is no button added to my control, I derive my MyButton class from Button
And that makes it act like a Button in some ways. But the click event a add has
nothing to do with it's derivation from Button.

0 Votes 0 ·

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered AN-5534 edited

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.



1.png (7.2 KiB)
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Will this change the arg type of the MyButton.Click object?

0 Votes 0 ·

@AN-5534 The Click event is not in the UserControl Event,where do you want to use your MyButton.Click ?

0 Votes 0 ·

In the MainWindow of my Wpf application.

0 Votes 0 ·
Show more comments