Cookie Monster
Cookie Monster

Reputation: 646

c# - Understanding MouseEvent

I have some doubts regarding some Mouse Event. The ones I am talking about is Mouse_Click Event , PreviewMouse_Down event and MouseDown event. I have read the explanations about these events in MSDN and yet I am having a hard time differentiating these 3.

So far from what I know:

Click : Occurs when the control is clicked by the mouse.
Preview: Occurs when any mouse button is pressed while the pointer is over this element.
Mousedown: Occurs when the mouse pointer is over the control and a mouse button is pressed.

For example, I don't know which event is suitable for which.Based on the explanations, they seem pretty much the same to me.On top of that, we have PreviewMouseDown which occurs when left mouse button is pressed.Aren't they all the same? Can anyone shed some light on this?

Upvotes: 0

Views: 3711

Answers (2)

Jan Paolo Go
Jan Paolo Go

Reputation: 6522

Aren't they all the same?

Short answer is "They are all different but depending on the use case, they may serve the same function."

Can anyone shed some light on this?

So how are they different?

First, let's look at what base classes the events are defined from.

MouseDown and PreviewMouseDown are from System.Windows.UIElement
Click is from System.Windows.Controls.Primitives.ButtonBase.

UIElement is a higher abstraction than ButtonBase which means that more classes are derived from it. In other words more classes will have the MouseDown and PreviewMouseDown events but not the Click event

Also, note that the ButtonBase itself derives from UIElement thus ButtonBase also has the MouseDown and PreviewMouseDown events.

But as stated in documentation of ButtonBase.Click:

The ButtonBase marks the MouseLeftButtonDown event as handled in the OnMouseLeftButtonDown method and raises the Click event. Hence, the OnMouseLeftButtonDown event will never occur for a control that inherits from ButtonBase.

Which brings us to the second point, how/when exactly are the events fired on a ButtonBase?

Please note that the following explanation considers only the three events (PreviewMouseDown, MouseDown, and Click). Other events may be involved before/between/after the said three events but are disregarded for discussion purposes.

PreviewMouseDown will always be the first to fire as soon as whichever mouse button (left or right) is pressed down.

MouseDown will occur next when using right click.

Click will occur next when using left click if and only if the mouse left button was also released on the ButtonBase

To add, when ButtonBase is in focus and the user presses ENTER or the SPACEBAR, Click will fire but not PreviewMouseDown

There's a whole new discussion on the difference between MouseDown and PreviewMouseDown events (basically XXX event and PreviewXXX event). You might want to read the documentation for Routed Events to get you going on that topic.

Upvotes: 3

JasonRho
JasonRho

Reputation: 106

As far as I know, it is related with Event Tunneling and Bubbling.

There is an order of events.

Let's suppose you have a button. If you left click the button then you would get below events in turn.

1) Preview Mouse Left Button Down

2) Preview Mouse Down

3) Mouse Left Button Down

4) Mouse Down

5) Preview Mouse Left Button Up

6) Preview Mouse Up

7) Mouse Left Button Up

8) Mouse Up

For more detail, please refer to Event Tunneling and Bubbling deeply. http://csharphelper.com/blog/2015/03/understand-event-bubbling-and-tunneling-in-wpf-and-c/

Upvotes: 0

Related Questions