Introduction to WPF self-learning (iii) built-in routed events for WPF routed events

Source: Internet
Author: User

Have you ever thought about. NET already has an event mechanism, why not use. NET events directly in WPF to add routed events instead of events? The most intuitive reason is that a typical WPF application uses many elements to correlate and combine, and whether or not you remember mentioning two trees, logical tree Logicaltree, and visual tree VisualTree in the basic knowledge of WPF self-Learning (a) xam, what are they?

As an example:

The code above is the logical tree Logicaltree, where a grid is embedded with other controls or layout components, equivalent to the leaves in a tree. And what is the visual tree VisualTree? It is a tree in the structure of the leaves inside, with a magnifying glass to look at, in fact, the structure of the leaf is a tree structure

As an example:

Since WPF uses such a design concept, the routed event is specifically for WPF, its function is to be able to move an event from the trigger point along the tree up or down, the need to respond to this event to add a listener, there will be a corresponding response, of course, its delivery can be used to stop the code. Well, I've probably learned about some routed events. Let's take a look at the routed events and principles built into WPF, and then we'll create a routed event that belongs to you.

1. WPF built-in routed events

Create a new WPF project and place a button on the page. Then use the MouseDown event on the Window,grid,button label, such as

Add a post code

Debugging run, the right mouse button click, the following three dialogs will pop up in turn.

The Buttonmousedown event is triggered:

The Gridmousedown event is triggered:

The Windowmousedown event is triggered:

I clicked on the button, why does the grid and window also raise events? In fact, this is the mechanism of routed events, the events raised by the source element to the upper level of the element, Button->grid->window, which causes these elements to receive the event. ( Note that it must be the right mouse button, otherwise the event cannot be raised.) )

If you want the grid and window not to handle this event, just add e.handled = True in the Button_mousedown method. This means that the event has already been processed and the other elements do not need to process the event again.

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

Grid. AddHandler (Grid.mousedownevent, New Routedeventhandler (Grid_mousedown), true);

Here I think we should have a general understanding of routed events. Routed events are actually divided into two categories: Bubble events and Preview events (also known as tunneling events). The example above is the bubble event.

2. Built-in routed event learning summary:

A bubble event is the most common in a WPF routed event, which indicates that the event propagates from the source element to the visual tree until it is processed or reached the root element. This allows us to handle events for the top level objects of the source element. (e.g. MouseDown)

The preview event takes another way, starting at the root element and traversing the element tree down until the source element of the event is processed or reached. This allows the upstream element to intercept and process the event before it reaches the source element. Based on the naming convention, preview events are prefixed with preview (for example, PreviewMouseDown).

Bubble event and Preview event differences

Bubble event: Click on the button, first pop Up "button", then "Grid", and finally pop up "window".

Preview event: Click on the button, first pop up "window", then "Grid", and finally pop up "button".

See the difference in this order, then we join E. The timing of the handled=true will be different.

PS: I am also a beginner of WPF, if there is no place, welcome in the comment area to teach, study, in order to share, in order to improve.

Introduction to WPF self-learning (iii) built-in routed events for WPF routed events

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: info-contact@alibabacloud.com 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.