Event Aggregator : How to stop event from propagating further? - wpf

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.

Related

What can I do with Prism Event Aggregator that cannot be done using bubbling/tunneling?

Imagine a Visual Studio type user interface in which there is a left pane and a right pane. I need to send a message/event that originates in the left pane to an element in the right pane. As a WPF newbie, I have the following linked questions:
Can this be done using bubbling and tunneling events?
If the answer is 'yes', which is better: bubbling/tunneling, or Prism's event aggregator?
I use Event Aggregation to notify my 'panes' of events that happen and both need to respond.
So, for one example, I would subscribe both 'panes' to an event. Now, say I kick of a request to the database or webservice, and upon return of results I want to notify both 'panes' simultaneously that data has arrived. I can easily do this by publishing to my event, then both 'panes' will kick off whatever they need to.
I like this way, because then if I had another module dropped in the left 'pane', all I would need to do is subscribe to that event to respond to any data coming in.

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 :)

See what handled a routed event

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...

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