I am looking for a diagram that will serve as a reference to show the order of events and overrides that get called for WPF custom controls that derive from common classes such as FrameworkElement, UIElement, Control, ContentControl, Decorator, etc.
I am particularly interested in the events and overrides dealing with the process of the control becoming visible and ready to interact with. In other words, I'm not as interested in this diagram showing the events that could deal with the user interacting with the control after it is ready to be interacted with, such as things like MouseLeftButtonDown.
What I am looking for is something like this diagram (diagrams like these explain the lifecyle of a WPF window or application) but for custom controls.
I've also seen this page, which, in my opinion makes it seem too simplistic. That page seems to basically say that there's mostly just the Initialized
, Loaded
, and Unloaded
events. To illustrate, a few of the general events/overrides I've needed to make use of in custom controls include:
Loaded
UnLoaded
IsVisibleChanged
OnApplyTemplate
Along with slightly more specialized overrides like Popup's OnOpened
and Expander's OnExpanded
, etc.
The problem I am having is that I forget when I can do things like VisualStateManager.GoToState(...)
(OnApplyTemplate
is the first real chance I believe). And I also keep discovering events that I need to make use of, such as the IsVisibleChanged (this event proved necessary when I needed a control to do a "reload/refresh" state change when the user returns to the Tab that contains my control). So I keep wondering are there yet other such events/overrides that I should really be paying attention to for a custom control? This is where a diagram that shows these things in relation to each other will help.
Considering these types of events/overrides, this Silverlight chart is pretty close to what I'm looking for, but for WPF. Some of the commenters on that post say that WPF's events/overrides are different than Silverlight's. If so, can you please point me to a similar diagram?
To further illustrate the need for such a diagram, consider the following sequence of events that I just witnessed on a custom control that derives from ContentControl:
Initialized
event callback calledLoaded
event callback calledIsVisibleChanged
event callback calledOnApplyTemplate
override calledLoaded
event callback calledLoaded
event callback called againLoaded
event callback called third time in a rowI doubt such a comprehensive diagram exists. How about you just make it yourself? Override all the methods and/or events your are interested in and put a Trace.WriteLine(new StackFrame(1).GetMethod().Name);
in each override. Your output will tell you in which order they were called.
Unloaded is not normally called unless you are in a TabItem and you swap to another one.
The loaded/unloaded events are not called when you would expect unfortunatly, and this makes it tricky to tell when your control should dispose of it's resources. I have also never managed to find a list of the containers that unload and reload their contents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With