An in-depth conversation event of learning notes in the "WPF in layman"

Source: Internet
Author: User

The events for WPF are routed events, the environment for routing is the UI component tree (visual tree), and visual tree consists of elements of controls and controls that can be passed and processed inside the control. The other tree is logical, which contains only the layout controls and other controls, not the constituent elements of the control. So routed events are passed along the visual tree.

Traditional. The disadvantages of the direct event model in NET development

The event owner and responder must establish a subscription relationship, and if you want the event to pass to the outer control, you must write the event response chain manually, that is, each control subscribes to the event and passes it again to the other control.

Routed events

Like dependency properties, each routed event is defined as a static readonly decorated RoutedEvent Type field, and the field name adds an event suffix. Adds the Add, remove event wrapper for the CLR event at the same time, and the type is Routedeventhandler. Sample code to listen for routed events:

// C # code New Routedeventhandler (Button_Click)); // XAML<stackpanel x:name="root" button.click="Button_Click  ">
To create a custom routed event

Declares the static readonly decorated RoutedEvent Type field, creates the CLR event wrapper for the routed event, and activates the routed event when appropriate.

     Public classReporttimebutton:button { Public Static ReadOnlyRoutedEvent reporttimeevent = eventmanager.registerroutedevent ("Reporttime", Routingstrategy.bubble,typeof(Routedeventhandler),typeof(Reporttimebutton));  Public EventRoutedeventhandler Reporttime {add { This. AddHandler (reporttimeevent, value); } Remove { This. RemoveHandler (reporttimeevent, value); }        }        protected Override voidOnClick () {Base.            OnClick ();  This. RaiseEvent (NewRoutedEventArgs (Reporttimeevent, This)); }    }

The name of the registered routed event is the same as the name of the CLR wrapper, and the Add method and the Remove method of the wrapper are invoked when the CLR event is appended or removed with the + = or-= operator. The types of routes can be divided into bubble: bubbling events, routed from the event activator to the higher level. Tunnel: Fires a pass from the root of the UI tree to the event. Direct: Simulates CLR events, does not route, and is delivered directly to the event handler. If you want the routed event to stop passing, simply set RoutedEventArgs's handled property to true, indicating that the event has been processed and will not be passed. Using Routedeventargs.originalsource to get the original object that triggered the event (which can be obtained into the control's internal component), Routedeventargs.source gets the previous event recipient in the event route.

An in-depth conversation event of learning notes in the "WPF in layman"

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.