Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no MouseMoveEvent -- or, how to use AddHandler for the mouse move event

The UIElement class defines static RoutedEvent members MouseLeftButtonDownEvent and MouseLeftButtonUpEvent -- but there is no MouseMoveEvent. As far as I can tell, neither does any class in the framework hierarchy. There is the regular event definition:

public event MouseEventHandler MouseMove;

So you can write:

void AttachHandler(UIElement element)
{
    element.MouseMove += OnMouseMove;
}

but you can't use the other form, which allows you to subscribe to even handled events:

void AttachHandler(UIElement element)
{
    element.AddHandler(UIElement.MouseMoveEvent, new MouseEventHandler(OnMouseMove), true);
}

So my question is twofold:

  1. Why is there no MouseMoveEvent defined anywhere?
  2. Is there a workaround that allows you to get a notification for MouseMove events even when they are handled?

Edit

I see that the MSDN docs acknowledge this as a limitation:

A limitation of this technique is that the AddHandler API takes a parameter of type RoutedEvent that identifies the routed event in question. Not all Silverlight routed events provide a RoutedEvent identifier, and this consideration thus affects which routed events can still be handled in the Handled case.

Edit #2

Per @HansPassant, the general answer is that "MouseMove" events cannot be marked as "handled", thus they always bubble. This is true of the TextBox, except for an apparent edge case: when you click on the TextBox's text area, thus activating the drag-to-select thingo, the "MouseMove" events no longer get triggered. I have no idea why that would be.


Note -- for anyone curious -- I am trying to write a behavior that allows the user to drag/drop a TextBox. The TextBox control intercepts mouse events by default, in order to allow text selection.

like image 344
McGarnagle Avatar asked Oct 20 '22 08:10

McGarnagle


1 Answers

It is explicitly mentioned in the MSDN article:

MouseMove cannot be used with AddHandler because there is no Handled in its event data

So that answers your questions:

Why is there no MouseMoveEvent defined anywhere?

Because none is needed.

Is there a workaround that allows you to get a notification for MouseMove events even when they are handled?

You don't need one, they can't be handled and thus always bubble. The Window's MouseMove event handler will see them.

like image 134
Hans Passant Avatar answered Oct 23 '22 09:10

Hans Passant