Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

WPF continues to respond to method steps marked as handled events

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge about "WPF continues to respond to the method steps marked as processed events". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

In WPF, bubbling or tunneling events are passed up the visual tree along with their inter-layer relationships, but some events pass to certain controls and "terminate"(no longer respond to the corresponding registration event), giving the impression of an event terminator. For example: textbox vs. mousdown event.

Cause: When the event handler arrives at the control, its event object property Handled is marked True. When the WPF event engine processes the event corresponding to the control, if it detects that the property is True, it will not call the corresponding handler. That is, after the WPF routing event is marked as handled, it is not that it is not passed on the visual tree; rather, the event engine no longer calls the handler of this event.

If you still want to process the responding event in its upper layer element (upper layer is relative to the direction of transmission of the event), the solution:

1, if the upper control can register the corresponding event. That is, the Template property of the corresponding control is not overridden. Code directly:

private void txt_MouseDown(object sender, MouseEventArgs e) { MessageBox.Show("TextMouseDown event"); e.Handled = false;//bubble to continue uploading } private void Grid_MouseDown(object sender, MouseEventArgs e) { MessageBox.Show("GridMouseDown event"); }

2. When customizing the control template, the binding template event does not take effect. At this time, the above method is no longer effective. For example: Custom list control template style

ScrollViewer In the control template, ScrollViewer's MouseButtonDown event handles events as follows: Breakpoint settings will find that mouse clicks do not trigger.

private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { //e.Handled = false ; }

Workaround: UIElement.AddHandler method: Adds a routing event handler for the specified routing event and adds the handler to the handler collection for the current element. For details, please refer to docs.microsoft.com/zh-cn/dotnet/api/system.windows.uielement.addhandler? f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DZH-CN%26k%3Dk(System.Windows.UIElement.AddHandler);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5);k(DevLang-csharp)%26rd%3Dtrue&view=netframework-4.8

Because an element marks the event as handled during delivery on the WPF event visual tree, the event no longer continues to respond when delivered (reason: Handled is marked True) You can use this method if you want subsequent elements to respond to this method.

So we can add the following code to the UserControl constructor above: indicates that gridMain handles the corresponding mouse click event

public UserControl() { InitializeComponent(); gridMain.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(MouseLeftButtonDown), true); }

Debug the breakpoint MouseLeftButtonDown again and you'll see that the breakpoint hits.

The key point of AddHandler is the last true, which tells the WPF engine to call the handle, even if it is marked Handled=true. However, the element above it will not respond after processing, because the handle is still marked processed. It can be seen that after WPF routing events are marked as handled, they are not passed on the visual tree; instead, they do not call the handler.

In the example above, if you want UserControl to continue responding, you are in the same situation as 1, just mark handle false.

"WPF continues to respond to method steps marked as processed events" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report