When Raising an MouseButtonEvent from a UserControl the MainWindow have no access to MouseButtonEventArgs?

Anyway, if you define an event to use a RoutedEventHandler it is hardly surprising that you'll end up being stuck with a RoutedEventHandler. You declared it like this: public static readonly RoutedEvent MyButtonDownEvent = EventManager. RegisterRoutedEvent("MyMouseButtonDown", RoutingStrategy.

Bubble, typeof(RoutedEventHandler), typeof(MyUserControl)); Notice the bit where it says typeof(RoutedEventHandler)? If I am not mistaken your code should look like this instead: public static readonly RoutedEvent MyButtonDownEvent = EventManager. RegisterRoutedEvent ("MyButtonDown", RoutingStrategy.

Bubble, typeof(MouseButtonEventHandler), typeof(MyUserControl)); public event MouseButtonEventHandler MyButtonDown { add { AddHandler(MyButtonDownEvent, value); } remove { RemoveHandler(MyButtonDownEvent, value); } } Example of how to propagate an existing MouseDown event to the custom event: InitializeComponent(); this. MouseDown += (s, e) => { RaiseEvent(new MouseButtonEventArgs(e. MouseDevice, e.

Timestamp, e. ChangedButton) { RoutedEvent = MyButtonDownEvent }); }.

Thank you for your effort – Jean-Marie Feb 15 at 14:21 thank you for your help . To reply to your comment "Why do you define your own MouseDown event while UserControl already has a normal MouseDown event? ".

I did not find the way to do that using only XAML in the MainWindow to subscribe to the Event coming from the UserControl. I also tested your code and noticed that the XAML considers the type is mismatched. Any idea?

– Jean-Marie Feb 15 at 14:46 No, I tried to fix it for some time but didn't work it out. If you don't mind not registering it as a RoutedEvent there should be no problem (you'd need to get rid of the setter and getter for the event too) – H.B. Feb 15 at 16:10 Thanks again for your help, I found 3 things: a) Your declaration for registering the event was RoutingStrategy. Bubble, typeof(MouseButtonEventArgs),typeof(MyUserControl)); I think it should be typeof(MouseButtonEventHandler).

Am I right? 2) After that I found that the XAML compiler was not complaining when the event consumer code behind changes the RoutedEventArgs to MouseButtonEventArgs e 3) It seems that RaiseEvent(e) does nothing but RaiseEvent(new RoutedEventArgs(MyButtonDownEvent)) give an exception trying to convert RoutedEvent to MouseButtonEvent. Any ideas?

– Jean-Marie Feb 15 at 17:31 "RoutedEvent there should be no problem (you'd need to get rid of the setter and getter for the event too)". Could you please give me a code snippet based on what you wrote yesterday, then I will leave you alone :) – Jean-Marie Feb 15 at 17:35.

I think I finally got it (at least I hope): If I write in the code behind: public event EventHandler MyRightButtonDownHandler; public void MyRightButtonDown(object sender, MouseButtonEventArgs e) { MyRightButtonDownHandler(sender, e); } Then in the consumer (MainWindow) XAML: And in the consumer code behind: private void globalDb_MyRightButtonDownHandler(object sender, MouseButtonEventArgs e) { Console. WriteLine("x= " + e. GetPosition(null).

X + " y= " + e. GetPosition(null). Y); } Please tell me if you have a better solution (By design policy - rules established where I work - all the event handling of my application MUST appear in the XAML).

Thanks again for your help.

Found a solutio – Jean-Marie Feb 15 at 18:39 Yes, that's what I meant, it's far more simple than using routed events, of course sometimes your events need to be routed though – H.B. Feb 15 at 18:56.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions