Idle Talk: the 19th of WPF (the transfer event in WPF [1])

Source: Internet
Author: User

[Transfer events]

WPF adds a lot of infrastructure on top of. NET simple event notifications. The design of the transfer event allows the event to work well with the element tree. After an event occurs, it can be automatically passed up and down in the visual tree and logic tree. We do not need to add any additional code.

Passing events makes it unnecessary for us to focus too much on the visual tree, so encapsulation is very important for us to understand the element synthesis of wpf. For example, when we click a Button event, we actually click a ButtonChrome or TextBlock, that is, we click the content element of the Button. It is precisely because an event can be transmitted along the visual tree that the Button discovers and can process the event. Therefore, we can add any element to the Content of the Button without any impact on the event. If this event is not passed, you must manually write the code to trigger the Button click event when you click an element in the Button.

The implementation and behavior of passing events are similar to those of Dependency. Similarly, let's look at how to implement simple event transfer. In most cases, passing events is not more difficult than a common. NET event. Like the Dependency attribute, the. NET language (except XAML) itself does not understand the transfer target. These support is based on WPF APIs.

Public class Button
{
// Events passed
Public static readonly RoutedEvent ClickEvent;

Static Button ()
{
// Register an event
Button. DoubleClickEvent = EventManager. RegisterRoutedEvent ("Click ",
RoutingStrategy. Bubble, typeof (RoutedEventHandler), typeof (Button ));
...
}
 
//. NET event guarantee (optional)
Public event RoutedEventHandler Click
{
Add {AddHandler (Button. ClickEvent, value );}
Remove {RemoveHandler (Button. ClickEvent, value );}
}

Protected override void OnMouseLeftButtonDown (MouseButtonEventArgs e)
{
...
// Trigger event
RaiseEvent (new RoutedEventArgs (Button. ClickEvent, this ));
...
}
...
}

From the above implementation, we can see that there are many similarities between events and Dependency attributes. It also defines a static RoutedEvent member and registers events in the static constructor. For convenience, a common. NET event is encapsulated. Here, AddHandler/RemoveHandler is not derived from DependencyObject, but a higher level of base class System. Windows. UIElement. These two methods add/delete a delegate for the corresponding event. In OnMouseLeftButtonDown, we construct an event parameter, pass in the event source object this, and then call the RaiseEvent function.

[Event policies and processing functions]

When registering a WPF event, we need to select a policy for passing the event, which specifies the way the event is transmitted in the element tree. WPF supports the following three policies:

Tunneling: the event is first triggered by the root element, and then arrives at each element under the tree until the source element (or a handler can process this event to terminate the transfer ).

Bubbling: the event is first triggered in the source element, and then up until the root element (or a handler to process this event terminates the transfer.

Direct: events are only triggered by source elements. This is the same as a common. NET event, except for the event trigger.

In the above example, the registered Event Policy is Bubbling.

The parameters for passing event processing functions are the same as those for common. NET events. The first parameter System. Object indicates the elements attached to the processing function. The second System. EventArgs derived class provides the following four useful attributes:

Source: The original element of the event to be triggered in the logic tree.

OriginalSource: the original element that inspires an event in the visual tree.

Handled: Boolean value, indicating whether the event is processed.

RoutedEvent: actually passing event objects (such as Button. ClickEvent ). This method is useful when the same handler function is used to process multiple passing events and can be used to differentiate passing events.

Source and OriginalSource represent the logical tree and visual tree objects. This is conducive to some low-level control, but for some events, there is no need to distinguish them, the two values are the same.

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.