c# - OnPropertyChanged method is not firing -
in wp8 app, have few controls bind foreground color changing in codebehind. onpropertychanged not firing when user event happened.
i have defined binding "controlforeground" in textblock , radiobutton data template controls in it. trying change foreground color whenever user presses button. new color assignment not updating ui. missing here?
in xaml,
<textblock x:name="lbltilecolor" textwrapping="wrap" text="selected color:" foreground="{binding controlforeground, mode=twoway}"/> <textblock x:name="lbltilecolor2" textwrapping="wrap" text="app bg:" foreground="{binding controlforeground, mode=twoway}"/> <radiobutton x:name="accentcolor" ischecked="true" borderbrush="white" foreground="{binding controlforeground, mode=twoway}"> <radiobutton.contenttemplate> <datatemplate> <stackpanel orientation="horizontal"> <rectangle width="25" height="25" fill="{staticresource phoneaccentbrush}"/> <textblock width="10"/> <textblock x:name="lbldefaultaccent" text="default accent color" foreground="{binding controlforeground, mode=twoway}"/> </stackpanel> </datatemplate> </radiobutton.contenttemplate> </radiobutton> <button x:name="updatecolor" click="update_btn"/>
in c#,
public class colorclass : inotifypropertychanged { private solidcolorbrush _controlforeground; public solidcolorbrush controlforeground { { return _controlforeground; } set { _controlforeground = value; onpropertychanged("controlforeground"); } } public colorclass() { } public event propertychangedeventhandler propertychanged; protected void onpropertychanged(string name) { if (propertychanged != null) propertychanged(this, new propertychangedeventargs(name)); } } public class colorpage:phoneapplicationpage{ public observablecollection<colorclass> testcollection { get; private set; } public void update_btn(object sender, eventargs e){ testcollection.add(new colorclass() { controlforeground = new solidcolorbrush(colors.red) }); } }
for 2nd problem (not being able bind controls inside data template), because these controls use data context of parent template not data context of page.
to fix this, you'll have tell these controls element name data context , give full path of property.
<textblock x:name="lbldefaultaccent" text="default accent color" foreground="{binding datacontext.controlforeground, elementname=layoutroot, mode=twoway}"/>
as can see above have specify element name. in case bound using this.datacontext = colorclass
element name name of outer grid in xaml, defaulted layoutroot
Comments
Post a Comment