c# - PropertyChangedEventHandler does not work -
i have following label , slider in usercontrol xaml
<label x:name="labelvalx" content="{binding path=xvalue}" horizontalalignment="left" width="88" height="44"/> <slider x:name="sliderspeed" value="{binding slidevalue, mode=twoway}" horizontalalignment="left" margin="10,35,0,0" verticalalignment="top" width="173" height="53" minimum="10" maximum="100" />
and specific setgetaccvalues.cs class:
public class setgetaccvalues : usercontrol, inotifypropertychanged { public event propertychangedeventhandler propertychanged; private string _xval; public string xvalue { { return _xval; } set { if (value != _xval) { _xval = value; onpropertychanged("xvalue"); } } } public byte _slidevalue; public byte slidevalue { { return _slidevalue; } set { if (value != _slidevalue) { _slidevalue = value; onpropertychanged("slidevalue"); } } } protected virtual void onpropertychanged(string propname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propname)); if (propname.equals("slidevalue")) { speedaccmeter(slidevalue); } } }
an in other getaccnotifications.cs class, have following part, i'm defining xvalue string specific value:
y = ((double)(sbyte)value) / 64.0; y = math.round(y, 2); setgetaccvalues set = new setgetaccvalues(); set.xvalue = y.tostring();
the problem occurs when onpropertychanged triggered "xvalue" propname, propertychangedeventhandler remains null, when triggered "slidevalue" propname not null. why remain null in xvalue case ?.
i believe propertychanged event firing before datacontext finished loading.
you can listen datacontextchanged event in usercontrol, when new datacontext available, can set properties.
public accview() { initializecomponent(); this.datacontextchanged += ondatacontextchanged; this.datacontext = new setgetaccvalues(); } private void ondatacontextchanged(object sender, dependencypropertychangedeventargs dependencypropertychangedeventargs) { y = ((double)(sbyte)value) / 64.0; y = math.round(y, 2); (dependencypropertychangedeventargs.newvalue setgetaccvalues).xvalue = y.tostring(); }
Comments
Post a Comment