wpfevent-handlingrouted-events

Routed event class handler for LoadedEvent does not work for most classes


I'm trying to register handler for all Loaded events:

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);

Unfortunately my handler is only called for Window and descendants of DatePicker. It does not work for any other type.

Can anyone explain this behavior ? It's a bug, sadly they've chosen to ignore it.

Is there some other way to be notified about any new element in application/window ?


Solution

  • Perhaps you could define an AttachedProperty/Behavior<T> that uses FrameworkPropertyMetadataOptions.Inherits, and then specify it on your root element, and in the behaviour hook the Loaded/Unloaded events on each instance