Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raising WPF MouseLeftButtonDownEvent event

Tags:

events

wpf

raise

I am trying ot raise a MouseLeftButtonDownEvent by bubbling it up the Visual tree with the following code.

         MouseButtonEventArgs args = new MouseButtonEventArgs(Mouse.PrimaryDevice,0,     MouseButton.Left);            
        args.RoutedEvent = UIElement.MouseLeftButtonDownEvent;
        args.Source = this;
        RaiseEvent(args);

For some reason the higher level components are not receiving this bubbled event. Am I overlooking something or is it not possible to raise this Mouse event

like image 564
TheWommies Avatar asked Mar 10 '10 22:03

TheWommies


1 Answers

Your problem is that you are raising an event that does not bubble.

MouseLeftButtonDownEvent is defined as RoutingStrategy.Direct, which means it is routed to only the control receiving the event.

You want to use Mouse.MouseDownEvent event instead. UIElement and other classes internally convert this into a MouseLeftButtonDownEvent. Make sure you set e.ChangedButton to MouseButton.Left:

RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
{
  RoutedEvent = Mouse.MouseDownEvent,
  Source = this,
});
like image 123
Ray Burns Avatar answered Sep 28 '22 09:09

Ray Burns