No-nonsense WPF series 13: routing events

Source: Internet
Author: User
Tags mscorlib

Logic tree

 
<WINDOW> <grid> <button> <stackpanel> <image/> <textblock/> </stackpanel> </button> </GRID> </WINDOW>

But in fact, these elements will be extended to the tree at runtime.

 
 
 
 

Event Routing

It is necessary to understand the logic tree and the visual tree, because the routing event is routed based on the visual tree. Routing events support three routing policies: Bubble, tunnel, and direct.

A bubble event is most common. It indicates that an event spreads (propagates) from the source element to the visible tree until it is processed or reaches the root element. In this way, you can process events for objects at the upper level of the source element. For example, you can attach a button. Click to the embedded grid element for processing.ProgramInstead of directly attaching it to the button itself. A bubble event has a name indicating its operation (for example, mousedown ).

Tunnel events use another method, starting from the root element, traversing down the element tree until the source element of the event is processed or reached. In this way, the upstream element can be intercepted and processed before the event reaches the source element. According to naming conventions, a tunnel event has a prefix Preview (for example, previewmousedown ).

Direct events are similar to normal events in. NET Framework. The only possible handler for this event is the delegate associated with it.

Generally, if a tunnel event is defined for a special event, a bubble event is generated. In this case, the tunnel event is triggered first, starting from the root element, going down to the source element, and searching for the handler. Once it is processed or reaches the source element, it will trigger a bubble event, upstream from the source element, find the handler. Bubble or tunnel events do not stop routing only by calling the event handler. If you want to abort a tunnel or bubble process, you can use the event parameters you pass to mark the event as handled in the event handler.

Example:

 
<Window X: class = "deepxaml. mainwindow "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: Local =" CLR-namespace: deepxaml "xmlns: SYS =" CLR-namespace: system; assembly = mscorlib "Title =" mainwindow "Height =" 250 "width =" 450 "> <grid X: Name =" rootgrid "button. click = "rootgrid_click"> <button X: Name = "btnok" margin = "30"> OK </button> </GRID> </WINDOW>

 

BackgroundCode:

Private void rootgrid_click (Object sender, routedeventargs e) {MessageBox. show (E. source as frameworkelement ). name); // btnok MessageBox. show (E. originalsource as frameworkelement ). name); // btnok}

 

Source refers to the Source Path of the logictree, and orginalsource refers to the Source Path on the visualtree.

 

Custom route event example

<Window X: class = "deepxaml. mainwindow "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: Local =" CLR-namespace: deepxaml "xmlns: SYS =" CLR-namespace: system; assembly = mscorlib "Title =" mainwindow "Height =" 250 "width =" 450 "> <grid X: Name =" rootgrid "> <stackpanel X: name = "stp1" Local: testbutton. clicktimeevent = "timehanler"> <stackpanel X: Name = "stp2" Local: testbutton. clicktimeevent = "timehanler"> <stackpanel X: Name = "stp3" Local: testbutton. clicktimeevent = "timehanler"> <ListBox X: Name = "ListBox"> </ListBox> <local: testbutton. clicktimeevent = "timehanler" Height = "50" margin = "30"> OK </local: testbutton> </stackpanel> </GRID> </WINDOW>
 background Code 
Using system; using system. collections. generic; using system. windows; using system. windows. data; using system. windows. documents; using system. windows. controls; namespace deepxaml {public partial class mainwindow: window {public mainwindow () {initializecomponent ();} private void timehanler (Object sender, timeeventargs e) {frameworkelement element = sender as frameworkelement; string strtime = E. clicktime. tolongtimestring (); this. listBox. items. add (element. name + ":" + strtime) ;}} public class timeeventargs: routedeventargs {public timeeventargs (routedevent, object source): Base (routedevent, source) {} public datetime clicktime {Get; set;} public class testbutton: button {public static routedevent timeevent = eventmanager. registerroutedevent ("clicktimeevent", routingstrategy. bubble, typeof (eventhandler <timeeventargs>), typeof (testbutton); public event routedeventhandler clicktimeevent {Add {This. addhandler (timeevent, value);} remove {This. removehandler (timeevent, value) ;}} protected override void onclick () {base. onclick (); timeeventargs ARGs = new timeeventargs (timeevent, this); args. clicktime = datetime. utcnow; this. raiseevent (ARGs );}}}

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.