On my way to understanding WPF RoutedEvent handling, is this the way it works?
Say I have a Window with a Grid and a Button inside the Grid. My Button_Click event handler looks like this:
private void Button_Click(object sender, RoutedEventArgs e)
{
// Do stuff
e.Handled = true;
}
Calling e.Handled = true; stops the event bubbling up the visual tree.
This is where I'm beginning to get lost.
- If I understand correctly, not calling
e.Handled = True;could result in another event handler firing in theGridor theWindowif they had an event listener forClickactive? - Is there a point where the bubbling stops by itself?
- Is there a performance effect if I don't stop the event propagation?
- And the most important, should I, by default, mark the event handled?
No, it does not. It only indicates to subsequent event handlers on the route up the element tree that another event handler before has marked the event as handled.
Yes, but it depends. In some cases, controls will mark an event automatically as handeled, e.g.
Button, so in these cases event if other elements had handlers for such an event attached, they would not have been executed.Moreover, in XAML, if you add an event handler, it will only be executed if the event is not handled, yet. However, in code-behind, you can still add an event handler that is executed even if
e.Handledistrueusing theAddHandlermethod onUIElements and passingtrueto thehandledEventsTooargument.As said before, this is only possible in code-behind, there is no XAML syntax for it.
Yes, it stops at the root of the element tree, in the majority of cases e.g. a
Window.The event will still be propagated, that is how the routed event mechanism works. Of course, if other handlers are not invoked because the event is marked as handled, those lines are not executed, but that changes the functionality of your application, which is a different story. As a general rule, do not optimze prematurely. If there is a preformance issue anytime, then use a profiler to analyze your application, get reliable and meanigful data to find the hotspots and solve the issue there. There is no need or benefit in optimizing something beforehand that will not likely be a performance concern.
Again it depends on what you want to achieve. Suppose you have a
ListViewwith lots of items. It shows a scrollbar that can be operated using the mouse wheel. So when you start to scroll the mouse wheel, an event is raised that invokes scrolling the embeddedScrollViewer. In this case, the event is handled automatically. Why? If thisListViewwas part of a large UI Control with other parentScrollViewers they would scroll, too, if the event was not handled. Imagine multiple nested controls scrolling all at once, that would be terrible. In other scenarios there may be a need that parent controls also get the event.This is an excerpt from the documentation on When to Mark Events as Handled:
The full paragraph gives you a more detailed idea of what a significant and complete way is, but as you can see, there is no golden rule that you can apply, it dependes on your requirements and design.