5

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.

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • 1
    You can use `UIElement.PreviewMouseMoveEvent`. – Hamlet Hakobyan May 21 '14 at 18:39
  • @HamletHakobyan well, that doesn't exist either, in Silverlight. I guess this is just one of those features that Silverlight doesn't have. I'm still curious to know what the best workaround would be, if any. – McGarnagle May 21 '14 at 18:54
  • 1
    It is explicitly mentioned in the [MSDN article](http://msdn.microsoft.com/en-us/library/ms598899%28v=vs.95%29.aspx): "MouseMove cannot be used with AddHandler because there is no Handled in its event data" – Hans Passant May 21 '14 at 20:09

1 Answers1

3

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Ah thanks, I missed that. "They always bubble" is true, except for when the text-selection drag thing is active. That is, while the left mouse button is down, I don't get the MouseMove events bubbling up. An edge case obviously ... not clear to me why that happens, or if it's a bug. – McGarnagle May 21 '14 at 20:37