See what handled a routed event - wpf

Basically we have a huge project, and we have an event handler that sometimes is triggered and some others it isn't. I think this is because somewhere in the jungle of code, we're handling that event, so it doesn't bubble up to where we need it. Is there anyway to find out where is it being handled?
Thanks!

Did you try to use Snoop..... there is an event Tab that tells us where the event is bubbled to and where its handled...

Related

Event Aggregator : How to stop event from propagating further?

I am facing an issue where I need to stop propagation of event from event aggregator(Prism Framework).
Is there any way I could do it?
Say I have 10 handlers for an event. Now, i am checking some condition in second event handler and if that condition evaluate to true, i want to stop further processing of the event by rest of 8 event handler.
That isn't really the way the observer pattern works. The EventAggregator is great for decoupling classes - various things that are interested in a message can subscribe to receive it, and anything that wants can publish that message out. All without having to know (a) who is sending and (b) who is receiving.
You have no control of the order the message is received, or cancelling it once it has been sent.
You may be able to make use of the Subscription Filtering functionality of the EventAggregator, but it isn't something I've had to do before.

Add/remove EventHandler multiple times in WPF

If I add (or remove) the same EventHandler to an Event multiple times, is that "bad"? Is anything happening internally which would cause problems here? (I am implementing data validation on a TextBox, which will turn on/off a MouseDoubleClick handler as the TB content becomes in/valid.)
Nothing really happens when you try to unsubscribe from event multiple times.
When you want to subscribe to event with your handler you can make sure it's not already subscribed. You should do that. Because it's possible to subscribe multiple times. of course only if it's not something you want to do...
What happens when you subscribe to the event twice with same handler? It's simple... handler is twice in event's InvocationList so it's called twice. When you subscribe again then it's called three times... every time the event is raised subscribed Handlers in InvocationList are called.
You can look here ...as you can see... It's duplicate of at least two already asked questions. So there are many answers :)

Events routing in WPF

I have implemented a UserControl. Then I would like to handle an event that is originally handled by Window (keyboard press). What is the best way to route the event caught by another component (higher in the components' tree)?
Thanks in advance for the replies and hints!
Cheers
It depends on the event you're trying to access. If it's a Preview event and the Window is setting e.Handled to true you'll need to use the method Alex suggests to circumvent the Window's handling of the tunneling. If it is a bubbling event (i.e. KeyDown) you don't need to do anything special since bubbling events hit the handlers on child elements first and go up the visual tree so the Window handler won't occur until after your UC's.
One thing you need to be careful with using Key events is that the event is only going to get picked up by your UC in the first place if the Focus is on or inside of it. This isn't something you need to worry about with things like Mouse events since they start at a specific location in the tree.
I believe you cannot gurantee that.
Window class is wrapping Win32 message-based event model and this will be the only WPF entity which will have access to those information.
I suggest that you create an attached property (which will be used by the Window) and implement the routing of the events yourself so that controls could subscribe to.
You can attach the routed handler specifying that you want to handle handled messages as well:
this.AddHandler(routedEvent, handler, true);
where this is an UIElement or derived class.
However there may still be events (key presses in this case) which don't make it past the window, not sure.

Direct Routed Event question

A colleague and I were chatting about routed events, and we came up with some things that didn't make a lot of sense. Primarily, what is the purpose of a Direct event, and why are both LeftMouseButtonDown and PreviewLeftMouseButtonDown direct events as opposed to bubbling and tunneling respectively?
According to the documentation, the LeftMouseButtonDown (and preview) events appear to behave as bubbling and tunneling events, except that the event is raised and reraised as these events traverse the tree. Since direct events can only be handled by the source of the event, it follows that any UIElement could handle a direct event since each one is raising the event and is also therefore the source. (Unless reraising the event is different than raising?). Why are these then not tunneling and bubbling like so many other similar events?
Neither my colleague nor I could come up with a use case in which we would ever create a custom event using a direct strategy (although we came up with some for tunneling and bubbling), yet LeftMouseButtonDown, one of the most common events (if not THE most common) is using this strategy.
Anyone have any ideas about the rational behind this?
Thanks!
My guess would be performance and there are considerations for having multiple events for the same thing.
There is already an event for MouseDown, to route both that and LeftMouseDown wouldn't make much sense. Finally, which would fire first and would cancelling\handling the first prevent the other? It sounds like it would be too easy to handle the same event twice.

WPF RoutedEvents Being Handled By...?

I have a 3D application in WPF which basically is supposed to zoom in and out as the MouseWheel event is fired. I have attempted to subscribe everything possible, but can't find what it is which is handling it.
Is there a way to find out what is? Or is there a way to have the event not handled, or for an encompassing UIElement to get to this event before/after the one dealing with it?
If you want to get called even though someone else has already handled the event, try subscribing to the event using the UIElement.AddHandled(RoutedEvent, bool) function. Pass in true as the second argument to get called even if the event has been handled.
You can also try subscribing to the PreviewMouseWheel event to get called when the event is tunneling.

Resources