Checking if a RoutedEvent has any handlers

≯℡__Kan透↙ 提交于 2019-12-03 20:20:51

No it is not possible. Actually the code you have assumes that they are handling the event on your control itself but you are declaring a Bubble event not a Direct event so technically something can be listening to the event further up the element chain. Also, technically one doesn't need to use the CLR event to hook the event; one can just use the AddHandler method directly passing in your routed event and that is what someone would have to do to hook the event further up the chain. Lastly the CLR event won't be used if someone registers a class handler for the event (to be notified whenever that event is raised for any instance of your MyButtonClass). If you look at something like the UIElement.BuildRouteHelper you will see all the steps that WPF goes through to establish the event route and the objects that will be invoked when the event is raised.

If you really need to know if there are any listeners then you are better off creating a CLR only event and not a routed event. Then you can check if your delegate is non-null.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!