Introduction to WPF (iii) embedded routing events of WPF routing events, wpf Routing

Source: Internet
Author: User

Introduction to WPF (iii) embedded routing events of WPF routing events, wpf Routing

Have you ever wondered why the. NET event is replaced by a routing event instead of a. NET event in WPF? The most intuitive reason is that a typical WPF application uses many elements to associate and combine. Do you still remember to mention the two secrets in the basic XAM knowledge of WPF, what are the logical tree LogicalTree and visual tree?

For example:

The above code is the logic tree LogicalTree. Other controls or layout components are embedded in a Grid, which is equivalent to the leaves in a tree. What is the visual tree? It is the structure of the leaves in a tree. You can use a magnifier to check whether the structure in the leaves is also a tree structure.

For example:

Since such a design concept is used in WPF, routing events are especially generated for WPF. Its function is to spread an event from the trigger point along the tree up or down, when a listener is added to respond to this event, the corresponding response is triggered. Of course, the transfer of the listener can be stopped by code. Now, I have learned about some routing events. Next, let's take a look at the built-in routing events and principles of WPF, and then create a route event of our own.

1. Embedded routing events of WPF

Create a new WPF project and place a button on the page. Then use the MouseDown event on the Window, Grid, and Button labels, as shown in figure

Add post code

Debug and run. Right-click the button and the following three dialog boxes will pop up in sequence.

The ButtonMouseDown event is triggered:


The GridMouseDown event is triggered:


The WindowMouseDown event is triggered:


I clicked the button. Why does Grid and Window also trigger events? In fact, this is the routing event mechanism. The events triggered are uploaded from the source element to the upper-Layer Element step by step, and the buttons-> Grid-> Window elements receive the events. (Be sure to right-click the mouse; otherwise, the event will not be triggered .)

If you want Grid and Window to not handle this event, you only need to add e. handled = true; this indicates that the event has been processed, and other elements do not need to process the event.

Private void Button_MouseDown (object sender, MouseButtonEventArgs e)


MessageBox. Show ("Button is clicked! ");

E. Handled = true;


If you want the Grid to participate in event processing, you only need to give it AddHandler.

Grid. AddHandler (Grid. MouseDownEvent, new RoutedEventHandler (Grid_MouseDown), true );

Now I think you should have a rough idea about routing events. There are actually two types of routing events: bubble events and preview events (also called tunnel events ). The above example is a bubble event.

2. built-in routing event Learning summary:

A bubble event is the most common event in a WPF route. It indicates that the event spreads from the source element to the visible tree until it is processed or reaches the root element. In this way, we can process events for objects at the upper level of the source element. (For example, MouseDown)

Preview an event in another way, starting from the root element, traversing down the element tree until it is processed or arrives at the source element of the event. In this way, the upstream element can be intercepted and processed before the event reaches the source element. According to naming conventions, a Preview event has a prefix Preview (for example, PreviewMouseDown ).

Differences between bubble events and preview events

Bubble event: click on the Button. "Button" is displayed first, "Grid" is displayed, and "Window" is displayed ".

Preview event: click on the Button. "Window" is displayed first, "Grid" is displayed, and "Button" is displayed ".

When we see the difference in this order, the timing for adding e. Handled = true is also different.


PS: I am also a beginner of WPF. If anything is wrong, please give me more advice and study in the comments area. To share, please improve.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.