wpf - Why doesn't my dependency property change propagate to my usercontrol -
i'm building windows phone 8 app. have usercontrol contents should updated asynchronously. model implements inotifypropertychanged. when update value in model propagates textbox control should not contents of usercontrol. part of puzzle missing or not possible? here's reproduction scenario.
app page:
<phone:phoneapplicationpage x:class="bindingtest.mainpage"> <grid x:name="contentpanel" grid.row="1" margin="12,0,12,0"> <button click="button_click" content="click" horizontalalignment="left" margin="147,32,0,0" verticalalignment="top"/> <textblock horizontalalignment="left" margin="69,219,0,0" textwrapping="wrap" text="{binding bar}" verticalalignment="top" height="69" width="270"/> <app:mycontrol x:name="snafu" horizontalalignment="left" margin="69,319,0,0" title="{binding bar}" verticalalignment="top" width="289"/> </grid> </phone:phoneapplicationpage>
this code behind model class (foo)
public partial class mainpage : phoneapplicationpage { foo foo; // constructor public mainpage() { initializecomponent(); foo = new foo(); contentpanel.datacontext = foo; } private void button_click(object sender, routedeventargs e) { foo.bar = "gnorf"; } } public class foo : inotifypropertychanged { string bar; public event propertychangedeventhandler propertychanged; void onpropertychanged(string name) { if (propertychanged != null) propertychanged(this, new propertychangedeventargs(name)); } public foo() { bar = "welcome"; } public string bar { { return bar; } set { bar = value; onpropertychanged("bar"); } } }
the usercontrol xaml
<usercontrol x:class="bindingtest.mycontrol"> <textbox x:name="layoutroot" background="#ff9090c0"/> </usercontrol>
and code behind usercontrol
public partial class mycontrol : usercontrol { public mycontrol() { initializecomponent(); } public static readonly dependencyproperty titleproperty = dependencyproperty.register("title", typeof(string), typeof(mycontrol), new propertymetadata("", ontitlechanged)); static void ontitlechanged(dependencyobject d, dependencypropertychangedeventargs e) { mycontrol c = (mycontrol)d; c.title = e.newvalue string; } public string title { { return (string)getvalue(titleproperty); } set { setvalue(titleproperty, value); layoutroot.text = value; } } }
when run example, usercontrol textbox contain welcome. when click on button regular textbox updates gnorf usercontrol still displays welcome.
i discovered if bind usercontrol propertychanged event handler null when call set_datacontext returns. databinding infrastructure seems infer binding usercontrol one-time binding instead of regular one-way binding. ideas?
try this:-
<app:usercontrol1 x:name="snafu" title="{binding bar,mode=twoway}" />
i checked it..this work..:)
Comments
Post a Comment