1

I have been searching for such an explaination for a while and I couldn't have found any yet. The thing is that I would like to know the steps of initialization/creation process of a Wpf Control.

Lets take a simple Button and I would like to know what is happening when constructor gets called, when is the control added to visualTree/logicalTree, when does invocation of dependencyproperty values happen, when is ApplyTemplate() called?

I need this information to be able to write proper custom control and I think it will help others too in case they wonder why this.Template.FindName("test") always returns "null" when being called inside a constructor of a control.

Can anyone answer this or provide me with links of tutorials where the concept of initizaling a control is poperly explained?

Thanks :)

cyberist
  • 107
  • 1
  • 10
  • 3
    Install the .NET Framework source available from here.... http://referencesource.microsoft.com/netframework.aspx ... then you can debug to your heart's content – Colin Smith Dec 13 '12 at 12:26
  • @colinsmith thanks for that suggestion but debugging source code will confuse me even more because its overwhelming – cyberist Dec 13 '12 at 12:31
  • Try this as a starting point.... http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.loaded%28v=VS.95%29.aspx – Colin Smith Dec 13 '12 at 12:35
  • i just read that article. its not helping me much. – cyberist Dec 13 '12 at 13:22
  • 2
    There are two nice books: Pro WPF in C# 2010 - MacDonald, WPF Control Development Unleashed - Podila. It is worth reading them both. Try this http://msdn.microsoft.com/en-us/library/ms745025.aspx – Pavel Voronin Dec 13 '12 at 13:39

1 Answers1

1

Per this SO answer here

Sequence of events when a Window is created and shown

As requested, here is the sequence of major events in WPF when a window is created and shown:

  1. Constructors and getters/setters are called as objects are created, including PropertyChangedCallback, ValidationCallback, etc on the objects being updated and any objects that inherit from them

  2. As each element gets added to a visual or logical tree its Intialized event is fired, which causes Styles and Triggers to be found applied in addition to any element-specific initialization you may define [note: Initialized event not fired for leaves in a logical tree if there is no PresentationSource (eg Window) at its root]

  3. The window and all non-collapsed Visuals on it are Measured, which causes an ApplyTemplate at each Control, which causes additional object tree construction including more constructors and getters/setters

  4. The window and all non-collapsed Visuals on it are Arranged

  5. The window and its descendants (both logical and visual) receive a Loaded event

  6. Any data bindings that failed when they were first set are retried

  7. The window and its descendants are given an opportunity to render their content visually

Steps 1-2 are done when the Window is created, whether or not it is shown. The other steps generally don't happen until a Window is shown, but they can happen earlier if triggered manually.

Also, I personally found the DispatcherPriority Enum useful in determining the event order in some cases

  • Invalid
  • Inactive
  • SystemIdle
  • ApplicationIdle
  • ContextIdle
  • Background
  • Input
  • Loaded
  • Render
  • DataBind
  • Normal - Constructors run here
  • Send
Community
  • 1
  • 1
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • Hi Rachel, i still dont know when exactly should i do what at what step of initialization. for example i have a customcontrol that holds a property and 4 levels deeper there is a visual element that should get the value of that property. how and when do i message the visualElement that the property has been set. i dont want to use depenendecyproperty and bind through relativeSource, i also cant use FineName in ApplyTemplate because the customControl's ContentTemplate is composed together on initialization. So at what step and how do i solve this? – cyberist Dec 13 '12 at 17:43
  • @cyberist Perhaps you can edit your question (or create a new SO question) with a small sample of your code demonstrating your problem? I'm not sure I understand what you're asking, but perhaps the [Dispatcher](http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx) is what you're looking for? – Rachel Dec 13 '12 at 17:52
  • its too much code. which part of the example didnt you get? its a customcontrol called "MyCustomControl" and it has its ControlTemplate. Inside the ControlTemplate there is a Button that has its custom ControlTemplate. In that Template there is an element that needs the value of a property that MyCustonControl owns. How do i do that? At what step of initializatin do i give the value? – cyberist Dec 13 '12 at 18:16
  • @cyberist Well ideally I'd do that sort of thing with a binding, but if you want to do it in the code behind you can probably do it in the `Loaded` event, since that runs after `Render` – Rachel Dec 13 '12 at 18:30
  • it should have be done before rendering even before measure override.. what step of initialization is that? – cyberist Dec 13 '12 at 18:35
  • @cyberist Since it's in a Template, it won't get rendered until the control using the template gets rendered, so I don't think you can do it before then. It might be possible to find the Template in the Resources and modify it from the code-behind, but I'm not certain of that. – Rachel Dec 13 '12 at 18:37
  • Thanks Rachel, I'll figure something out. – cyberist Dec 13 '12 at 18:55