Introduction to WPF (4) custom routing events of WPF routing events, wpf Routing

Source: Internet
Author: User

Introduction to WPF (4) custom routing events of WPF routing events, wpf Routing

In the previous blog, we wrote a built-in routing event. In addition to the built-in routing events, we can also customize routing events. Next, let's take a look at how to create custom routing events in WPF.

There are three steps to create a custom route event:

1. Declare and register a route event.

2. Use CLR events to encapsulate routing events (encapsulate routing events ).

3. Create a method to stimulate routing events.

Now let's create a routing event that can report the current time and current location information, and visit it in the control. Now start creating custom route events

1. Declare custom routing events

Create a derived class that inherits the RoutedEventArgs class ReportCurrentLocationEventArgs to carry time and location messages, ClickTime to store time, And CurrentLocation to store location

 

Ii. Define registration route events

The EventManager. RegisterRoutedEvent method is used to register four parameters. The Code is as follows:

Public static readonly RoutedEvent ReportCurrentLocationEvent = EventManager. RegisterRoutedEvent

("ReportCurrentLocation", RoutingStrategy. Bubble, typeof (EventHandler <ReportCurrentLocationEventArgs

>), Typeof (ButtonReportCurrentLocation ));

The first parameter is the Name of the routing event.

The second parameter is the routing event transmission mode. There are three methods:

The first mode is the Bubble mode. This mode is passed from the trigger point up to know the outermost layer.

The second is that Direct is the same as traditional events and does not pass through the element tree.

The third mode is the preview mode (Tunnel mode), which is opposite to the bubble mode and passed down.

The third parameter is the routing event Processor type, and the passed parameter is a custom class.

The fourth parameter is the type of the route event.

 

3. encapsulate routing events

The CLR event encapsulate is different from the GetValue and SetValue of the dependency attribute. Here, the Add and Remove functions are used to allocate event handlers to routing events.

Public event RoutedEventHandler ReportCurrentLocation

{

Add {this. AddHandler (ReportCurrentLocationEvent, value );}

Remove {this. RemoveHandler (ReportCurrentLocationEvent, value );}

}

4. Create a method to stimulate a routing event

Override the OnClick method to trigger the route setting event, which is triggered using the RaiseEvent () method.

Protected override void OnClick ()

{

Base. OnClick ();

ReportCurrentLocationEventArgs args = new ReportCurrentLocationEventArgs (ReportCurrentLocationEvent, this );

Args. ClickTime = DateTime. Now;

This. RaiseEvent (args );

}

 

Complete code is as follows:

 

In the interface XAML, the design is as follows:

 

 

Finally, let's take a look at what the event processor is like.

 

Running effect:

 

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: 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.