WPF events (one) built-in routed events

Source: Internet
Author: User

Original: WPF events (one) built-in routed events

Windows is a message-driven operating system, and the programs running on it are running, and as the object-oriented development platform matures, Microsoft encapsulates the message mechanism into an easier-to-understand event model with 3 key points: The owner of the event, the responder of the event, the subscription to the event

To reduce the amount of coupling and code generated by event subscriptions, WPF introduces a routed event mechanism that distinguishes routed events from traditional direct events in that the sender delivers the message directly to the event responder via an event subscription, using its event handler method to respond to the occurrence of the's really into event, The driver logic runs according to customer requirements, and there is no direct subscription relationship between the event owner of the routed event and the event responder, the owner of the event is only responsible for firing the event, who will respond to it and it is not known that the responder of time has an event listener installed and listens for a class of events when such events are passed. The event responder uses the event handler to respond to the event and determines whether the event can continue to be passed.

Capturing system routed events, most events in WPF are routable events, and routable events have the routed event information column in the MSDN documentation, for example, the interface code is as follows:

<window x:class= "Wpfapplication1.mainwindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation    "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "title=" MainWindow "height=" 202 "width=" 202 ">            <grid x:name= "GridRoot" background= "Lime" > <grid x:name= "Grida" margin= "ten" background= "Blue" >            <Grid.ColumnDefinitions> <columndefinition/> <columndefinition/> </Grid.ColumnDefinitions> <canvas x:name= "canvasleft" grid.column= "0" background= "Red" margin= "            "> <button x:name=" buttonleft "content=" left "width=" + "height=" margin= "                </Canvas> <canvas x:name= "Canvasright" grid.column= "1" background= "Yellow" margin= "ten" >        <button x:name= "Buttonright" content= "right" width= "" height= "" margin= "" Ten "/> </Canvas> </Grid> </grid></window> 

When a button is clicked, the event is rolled up to the backbone of the logical tree, and every control along the way receives the event, which can be captured if a control needs to monitor the event

First define a response function for a routed event:

        private void ButtonClicked (object sender, RoutedEventArgs e)        {            MessageBox.Show (E.originalsource as FrameworkElement). Name);        }

Then, associate the routed events that need to be cared for with this function

Gridroot.addhandler (Button.clickevent, New Routedeventhandler (buttonclicked));

If you only care about an event that occurs for a particular object, you can use E. OriginalSource gets the object of the event, it can be determined that the object triggered the event.


WPF events (one) built-in 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.