Routed events are more propagating events-they can bubble up and down in the element tree, and are handled by the event handlers along the propagation path.
Routed events can be used in a traditional way-by associating an event handler with the correct signature.
1. Defining, registering, and encapsulating routed events
As with dependency properties, routed events are represented by read-only static fields, registered in static constructors, and encapsulated by standard. NET event definitions.
1 Public Abstract classButtonbase:contentcontrol2 {3 //Defining Events4 Public Static ReadOnlyRoutedEvent clickevent;5 //Registering Events6 Staticbuttonbase ()7 {8Buttonbase.clickevent =Eventmanager.registerrutedevent (9 "Click", Routingstrategy.bubble,Ten typeof(Routedeventhandler),typeof(ButtonBase)); One } A}
Routed events are registered using the Eventmanager.registerroutedevent () method.
When registering, you need to specify:
- The name of the event
- Route type
- Delegate that defines the event handler syntax
- The class that owns the event.
Typically, routed events pass through the normal. NET events to make them accessible to all. NET languages. The event wrapper can use the AddHandler () and RemoveHandler () methods to add and remove registered callers, which are defined in the FrameworkElement base class and are inherited by each WPF element.
2. Shared routed events
1 Uielement.mouseupevent = Mouse.MouseUpEvent.AddOwner (typeof(UIElement));
UIElement (This class is the starting point for all common WPF elements) class reuses MouseUp events through the Routed-event.addowner () method.
3. Raising a routed event
Events do not pass through the traditional. NET event wrapper, instead of throwing it using the RaiseEvent () method, all elements inherit the method from the UIElement class
1 New This ); 2 base. RaiseEvent (e);
The RaiseEvent () method is responsible for raising events for each invoker that has been registered through the AddHandler () method.
All WPF events use the familiar. NET conventions for event signing, and each event handler's first argument (the sender argument) provides a reference to the object that raised the event, and the second argument eventargs the object, which is bound to other important additional details. For example, the MouseUp event provides a MouseEventArgs object that indicates which mouse was pressed when the event occurred:
1 Private void img_mouseup (object sender, MouseButtonEventArgs e)2{3 4 }
4. Handling Routed Events
The most common method is to add the event attribute to the XAML markup:
1 <image source="happyface.jpg" stretch="None"2 name="img"mouseup="img_mouseup" />
You can also connect events through code:
1 New Mousebuttoneventhandler (Img_mouseup);
2 //or
3 //img. MouseUp + = Img_mouseup;
5. Three ways in which routed events occur
- A direct routed event similar to a normal. NET event. Events originate from an element and are not passed to other elements, such as MouseEnter events;
- A bubbling routed event that is passed up in the containing hierarchy (bubbling event). For example, the MouseDown event, which is raised by the element that was clicked, is then raised by the element's parent element and then raised by the parent element until the top of the element tree;
- Tunnel routed Events (tunneling event) that are passed down in the containing hierarchy. For example, the Previewkeydown event, first at the window level, then the more specific container, until it reaches the element that has focus when the key is pressed.
6.routedeventargs class
name span> |
description |
source |
Indicates the object that raised the event |
originalsource |
|
routedevent |
Providing RoutedEvent objects for triggered events through event handlers |
handled strong> |
|
7. Additional Events
If the previous control does not support events for the next layer of control, and you need to add the same event-handling method to several controls in the next layer, you need to use the "Attach event"
1<stackpanelbutton.click="dosomething" margin="5">2<button name="cmd1">command1</Button>3<button name="CMD2">command1</Button>4<button name="cmd3">command1</Button>5<StackPanel>
This allows all button click events to be captured.
8. Life Cycle Events
(1) Life cycle events for all elements
Name |
Description |
Initialized |
Occurs when an element is instantiated and the element's properties have been set based on XAML markup |
Loaded |
The event occurs when the entire window has been initialized and the style and data bindings have been applied. |
Unloaded |
The event occurs when the element is disposed. |
(2) Life cycle events for window classes
- Sourceinitialized
- Contentrendered
- Activated
- Deactivated
- Closing
- Closed
(3) Keyboard events
- Previewkeydown
- KeyDown
- Previewtextinput
- TextInput
- Previewkeyup
- KeyUp
(4) Get keyboard status
- Iskeydown
- Iskeyup
- Iskeytoggled
- Getkeystates
(5) Mouse click
- Previewmouseleftbuttondown
- MouseLeftButtonDown
- Previewmouseleftbuttonup
- MouseLeftButtonUp
Routed events for the WPF Programming Toolkit (i)