Kylo Ren
Kylo Ren

Reputation: 8823

why no PreviewMouseEnter event is in WPF

Why there is no any such PreviewMouseEnter event is WPF?

question is generic also for other nonexistent Preview events.

OR

Why some events are direct event in WPF? Can't they make all events as routed events? This could have saved some extra programming we have to do. example : a TreeView

Were there any disadvantages to do so?

Upvotes: 0

Views: 853

Answers (1)

Szabolcs Dézsi
Szabolcs Dézsi

Reputation: 8843

It's because MouseEnter is not routed through the element tree.

There are three different routing strategies for routed events:

  • Bubbling: these are the events that are travelling upwards in the element tree from the source of the event, like bubbles in your pop. :)

  • Tunneling: these are the events that are travelling downwards in the element tree. These are the ones prefixed with Preview.

  • Direct: only the source has the chance to handle the event. The MouseEnter uses this routing strategy.

The overview HERE probably gives you a better understanding than my answer.

Update: Theoretically as I see there is nothing that would have prevented the implementation of MouseEnter as a bubbling or tunneling event. I think it was just a design decision of the developers of WPF. I was able to find one reference in the book "Programming WPF: Building Windows UI with Windows Presentation Foundation" which talks about the MouseEnter event specifically.

Direct events work like normal .NET events: only handlers attached directly to the originating element are notified - no real routing occurs. This is typically used for events that make sense only in the context of their target element. For example, it would be unhelpful if mouse enter and leave events were bubbled or tunneled - the parent element is unlikely to care about when the mouse moves from one child element to another. At the parent element, you would expect "mouse leave" to mean "the mouse has left the parent element", and beacuse direct event routing is used, that's exactly what it does mean. If bubbling were used, the event would effectively mean "the mouse has left an element that is inside the parent, and is now inside another element that may or may not be inside the parent," which would be less useful.

Source

Upvotes: 4

Related Questions