0

I'm trying to create a generic event, so all my Labels have this event. I can easily create a doubleclick and mouseenter event as follows.

Label l = new Label();

Double Click

l.AddHandler(Label.MouseDoubleClickEvent, new RoutedEventHandler(lbl_Click));

Mouse Enter

l.MouseEnter += new MouseEventHandler(lbl_MouseEnter);

How can I create a click up right dynamic event? I'm trying the following way.

l.MouseRightButtonUp += new MouseButtonEventArgs(lbl_MouseRightButtonUp);

l.AddHandler(Label.MouseRightButtonUpEvent, new MouseButtonEventArgs(lbl_MouseRightButtonUp));

and it's not working, how do I set the click event?

Emiry Mirella
  • 567
  • 1
  • 7
  • 21
  • Right mouse button up? You want left [click](https://stackoverflow.com/q/43542570/1997232), aren't you? – Sinatr Aug 02 '18 at 13:52
  • Actually I need both of them, left and right. – Emiry Mirella Aug 02 '18 at 13:56
  • I can not use xaml to create the event, I have to create it in the source code – Emiry Mirella Aug 02 '18 at 13:58
  • Instead of creating it in code, use `DataTemplate` and instead of the looping or creating event handler look into `EventSetter`. That should cover it. – XAMlMAX Aug 02 '18 at 14:08
  • *"so all my Labels have this event"* - you can add default style for all `Label`s like [this](https://stackoverflow.com/q/15056027/1997232), thanks @XAMlMAX for idea. – Sinatr Aug 02 '18 at 14:18
  • So for example, is it possible for me to create a style in my ResourceDictionary and add it to my element via the code? – Emiry Mirella Aug 02 '18 at 14:24
  • 1
    yup, not only that you can assign the handler in the xaml as well, so it looks for the handler and then executes the method, you could look into interaction so you can use commands and do MvvM while you're at it. [SO example here](https://stackoverflow.com/a/4897897/2029607). – XAMlMAX Aug 02 '18 at 14:45
  • @XAMIMAX i am using eventsetter, but not work for me https://stackoverflow.com/questions/51659165/adding-eventsetter-methods-in-class – Emiry Mirella Aug 02 '18 at 18:13
  • That's not a problem, you can have the style in your App.xaml but put the event setter in your Main Window. This way you can have handlers for them. I'll create a gist for you with an example of how you can do it. – XAMlMAX Aug 03 '18 at 07:09
  • I have looked at you `App.xaml` and the xaml is invalid. but I think you know that. [Here is the gist](https://gist.github.com/jaworekplay/1b4c349cbe07f4555d69a3eb3ade06b6). – XAMlMAX Aug 03 '18 at 07:23

0 Answers0