Raising WPF MouseLeftButtonDownEvent event

TheWommies picture TheWommies · Mar 10, 2010 · Viewed 16.8k times · Source

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

Answer

Ray Burns picture Ray Burns · Mar 11, 2010

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,
});