wpf - LoadedEvent 的路由事件类处理程序不适用于大多数类

标签 wpf event-handling routed-events

我正在尝试为所有 Loaded 事件注册处理程序:

EventManager.RegisterClassHandler(typeof(UIElement), FrameworkElement.LoadedEvent, new RoutedEventHandler(OnRoutedEvent), true);
EventManager.RegisterClassHandler(typeof(ContentElement), FrameworkContentElement.LoadedEvent, new RoutedEventHandler(OnRoutedEvent), true);
EventManager.RegisterClassHandler(typeof(UIElement), FrameworkElement.UnloadedEvent, new RoutedEventHandler(OnRoutedEvent), true);
EventManager.RegisterClassHandler(typeof(ContentElement), FrameworkContentElement.UnloadedEvent, new RoutedEventHandler(OnRoutedEvent), true);

不幸的是,我的处理程序只为 Window 和 DatePicker 的后代调用。它不适用于任何其他类型。

谁能解释这种行为?这是一个 bug ,可悲的是他们选择忽略它。

是否有其他方法可以通知 application/window 中的任何新元素?

最佳答案

也许你可以定义一个 AttachedProperty/Behavior<T>使用 FrameworkPropertyMetadataOptions.Inherits ,然后在您的根元素上指定它,并在行为 Hook 中 Loaded/Unloaded每个实例上的事件

关于wpf - LoadedEvent 的路由事件类处理程序不适用于大多数类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11455800/

相关文章:

c# - 为什么没有 MouseMoveEvent——或者,如何为鼠标移动事件使用 AddHandler

java - 保持事件触发点击事件

c# - WPF/MVVM : Need help to fix a broken binding with ContentControl + DataTemplate

c# - WPF 数据网格和 ITypedList

c# - ObservableCollection 和 ListBoxItem DataTemplate 生成问题

javascript - 当前文档处于设计模式时是否可以捕获 KeyDown 事件?

java - 什么是 sun.awt.windows.WToolkit?

c# - WPF 中的 MouseEnter 和 MouseLeave 处理(使用 VS2008 和 .net 3.5)未按预期工作

c# - 使用带有 Caliburn micro Message.Attach 的附加事件

wpf - 捕获线程、任务、计时器、ThreadPool 线程和后台工作线程上引发的 UnhandledExceptions