c# - DependencyProperty's value is null when method in attached property's class is called -



c# - DependencyProperty's value is null when method in attached property's class is called -

we have been working finish day on problem , have summed little example. converting project silverlight wpf, in silverlight both versions work, in wpf 1 does.

we have simple command string-type dependencyproperty this:

public class mycontrol : command { public string text { { homecoming (string)getvalue(textproperty); } set { setvalue(textproperty, value); } } public static readonly dependencyproperty textproperty = dependencyproperty.register("text", typeof(string), typeof(mycontrol), new propertymetadata(null, textchanged)); private static void textchanged(dependencyobject d, dependencypropertychangedeventargs e) { } }

then have class attached property follows:

public class myattachedproperty { public static readonly dependencyproperty descriptionproperty = dependencyproperty.registerattached("description", typeof(string), typeof(myattachedproperty), new propertymetadata(null, descriptionpropertychanged)); public static string getdescription(dependencyobject obj, string value) { homecoming (string)obj.getvalue(descriptionproperty); } public static void setdescription(dependencyobject obj, string value) { obj.setvalue(descriptionproperty, value); } private static void descriptionpropertychanged(dependencyobject d, dependencypropertychangedeventargs e) { var mysuperbcontrol = d mycontrol; debug.writeline("the control's text is: " + mysuperbcontrol.text); } public static void donothing() { } }

we implement our command in mainwindow.xaml:

<contentcontrol x:name="mycontentcontrol"> <contentcontrol.contenttemplate> <datatemplate> <local:mycontrol x:name="mycntrl" text="defaulttext" att:myattachedproperty.description="test"/> </datatemplate> </contentcontrol.contenttemplate> </contentcontrol>

and in code-behind have constructor:

public mainwindow() { myattachedproperty.donothing(); initializecomponent(); }

if start project way, debug-text not contain text. if phone call donothing() after initializecomponent(), show text. can please explain, why? note, in silverlight both ways work. also, if not utilize command in datatemplate both ways work.

it's interesting side effect. makes sense when think dependencyproperty registration adds global collection. if phone call static constructor on myattachedproperty first added collection first , set first object.

if forcefulness static constructor run first on mycontrol adding same empty static method donothing can do

public mainwindow() { mycontrol.donothing(); myattachedproperty.donothing(); initializecomponent(); }

and text shown or in case

public mainwindow() { myattachedproperty.donothing(); mycontrol.donothing(); initializecomponent(); }

the empty text shown.

c# .net wpf xaml datatemplate

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -