c# - Notify change in calculated property -
there have been similar questions on site have not found answer works, post again.
public myclass { public person person1 {get; set;} public person person2 {get; set;} calculatedproperty { { return person1.salary + (person2.salary/2) ; } } } public class person { public double salary { get; set;} }
i want able notify changes in calculatedproperty
whenever person1.salary
and/or person2.salary
changes.
i have tried adding onpropertychanged("calculatedproperty")
on setters of person1
, person2
not work (and understand why), can't find way notify changes. please =)
(is there way use objectdataprovider
this?... been trying too...)
you need person
implement inotifypropertychanged
too, register 2 properties have. once propertychanged
invoked salary
on either of them, invoke propertychanged
on calculatedproperty
void personpropertychanged(object sender, propertychangedeventargs e) { if(e.propertyname == "salary") onpropertychanged("calculatedproperty"); }
just remember unregister when person changed.
update:
as jim said, setter should this:
private person _person; public person person { { return _person; } set { if (equals(value, _person)) return; if (_person != null) _person.propertychanged -= personpropertychanged; _person = value; if(_person != null) _person.propertychanged += personpropertychanged; onpropertychanged("person"); } }
Comments
Post a Comment