wpf - Order In Chaos Of Control Initialization Steps -
i know when happening inside initalization process of controls when start wpf application?
when dp initalized? when binding? when datacontext set? datacontext avaialbe in constructor of control? there kind of order?
i realized ran trap once set value on getter/setter of dp inside constructor of control dp value gets updated values gets rolled default value null.
so guess contructors initalized first , dependency properties.
can me out this?
edit: rachel. dp receives value 234 , immedialty rolls null. think because constructor gets called first , subsequently initalizing of dps happens sets dp null because null default value. thinking wrong this? order of initalization steps of control or dependency object.
class mysuperdupercoolclass : contentcontrol { public mysuperdupercoolclass() { initalizecomponents(); this.mysuperduperproperty = "234"; } public string mysuperduperproperty { { return (string)getvalue(mysuperduperpropertyproperty);} set { setvalue(mysuperduperpropertyproperty, value);} } public static dependencyproperty mysuperduperpropertyproperty = dependencyproperty.register("mysuperduperproperty", typeof(string), typeof(mysuperdupercoolclass), new propertymetadata(null)); }
i find dispatcherpriority enum useful recalling exact event order:
- send
- normal - constructors run here
- databind
- render
- loaded
- background
- contextidle
- applicationidle
- systemidle
- inactive
- invalid
- input
as can see, constructors run first, followed data bindings.
dependencyproperties
initialized when object gets created, other property, occur prior constructor being run property exists in constructor.
setting datacontext
property or other dependencyproperties
works other property setting. if set them binding, they'll evaluated after constructor. if set them in xaml, they'll set in constructor. if set them in loaded event, they'll set after has been constructed, bound, , rendered.
you might find this answer useful:
sequence of events when window created , shown
as requested, here sequence of major events in wpf when window created , shown:
constructors , getters/setters called objects created, including propertychangedcallback, validationcallback, etc on objects being updated , objects inherit them
as each element gets added visual or logical tree intialized event fired, causes styles , triggers found applied in addition element-specific initialization may define [note: initialized event not fired leaves in logical tree if there no presentationsource (eg window) @ root]
the window , non-collapsed visuals on measured, causes applytemplate @ each control, causes additional object tree construction including more constructors , getters/setters
the window , non-collapsed visuals on arranged
the window , descendants (both logical , visual) receive loaded event
any data bindings failed when first set retried
the window , descendants given opportunity render content visually
steps 1-2 done when window created, whether or not shown. other steps don't happen until window shown, can happen earlier if triggered manually.
edit based on code added question
your dependencyproperty.register
method looks funny me. signature of method doesn't match of overloads method, , you're using appears custom uiproperty
class set default value instead of normal propertymetadata.
i can confirm if code runs expected normal dependencyproperty.register
signature, cause of problem either somewhere within custom code, or how using/setting property.
the code used quick sample test this:
public partial class usercontrol1 : contentcontrol { public usercontrol1() { initializecomponent(); this.testdependencyproperty = "234"; } public string testdependencyproperty { { return (string)getvalue(testdependencypropertyproperty); } set { setvalue(testdependencypropertyproperty, value); } } public static dependencyproperty testdependencypropertyproperty = dependencyproperty.register("testdependencyproperty", typeof(string), typeof(usercontrol1), new propertymetadata(null)); }
and xaml is
<contentcontrol x:class="wpfapplication1.usercontrol1" x:name="testpanel" ...> <label content="{binding elementname=testpanel, path=testdependencyproperty}"/> </contentcontrol>