c# - Parent class notified if child class properties change -
i have view viewmodels, organizationcontact , personalinformationmodel.
organizationcontact uses personalinformationmodel.
they setup so:
organizationcontact:
public class organizationcontact : viewmodelbase { private personalinformationmodel _contactinfo; public personalinformationmodel contactinfo { { return _contactinfo; } set { _contactinfo = value; raisepropertychanged(nameof(contactheader), "", "", true); raisepropertychanged(nameof(contactinfo), null, _contactinfo, true); } } //generate header public string contactheader { { var header = ""; if (!string.isnullorwhitespace(contactinfo.title?.titleabbreviation)) { header += contactinfo.title.titleabbreviation + " "; } if (!string.isnullorwhitespace(contactinfo.firstname)) { header += contactinfo.firstname + " "; } if (!string.isnullorwhitespace(contactinfo.middleinitial)) { header += contactinfo.middleinitial + ". "; } if (!string.isnullorwhitespace(contactinfo.lastname)) { header += contactinfo.lastname + " "; } return header; } } public int organizationlink { get; set; } public string position { get; set; } public int priority { get; set; } }
personalinformationmodel:
public class personalinformationmodel : viewmodelbase { private string _firstname; private string _middleinitial; private string _lastname; private string _phonenumber; private string _phoneextension; private string _faxnumber; private string _email; public int personalidentity { get; set; } public string firstname { { return _firstname; } set { _firstname = value; raisepropertychanged(nameof(firstname), "", _firstname, true); } } public string middleinitial { { return _middleinitial; } set { _middleinitial= value; raisepropertychanged(nameof(middleinitial),"",_middleinitial,true); } } public string lastname { { return _lastname; } set { _lastname = value; raisepropertychanged(nameof(lastname), "", _lastname, true); } } public string phonenumber { { return _phonenumber; } set { _phonenumber = value; raisepropertychanged(nameof(phonenumber), "", _phonenumber, true); } } public string phoneextension { { return _phoneextension; } set { _phoneextension = value; raisepropertychanged(nameof(phoneextension), "", _phoneextension, true); } } public string faxnumber { { return _faxnumber; } set { _faxnumber = value; raisepropertychanged(nameof(faxnumber), "", _faxnumber, true); } } public string email { { return _email; } set { _email = value; raisepropertychanged(nameof(email),"",_email, true); } } public string fullname => $"{firstname} {lastname}"; }
personalinformationmodel used other classes.
what i'm looking way organizationcontact
informed if property inside of personalinformationmodel
changes contactheader
inside of organizationcontact
can notified of change.
okay, want you'll need few things. first, register propertychanged handler when set contactinfo property on organizationcontact:
public personalinformationmodel contactinfo { { return _contactinfo; } set { if (_contactinfo != null) { _contactinfo.propertychanged -= contactinfo_propertychanged; } _contactinfo = value; if (_contactinfo != null) { _contactinfo.propertychanged += contactinfo_propertychanged } raisepropertychanged(nameof(contactinfo), null, _contactinfo, true); } }
now, create handler. should able raise propertychanged event on contactheader update bindings.
void contactinfo_propertychanged(object sender, propertychangedeventargs args) { raisepropertychanged(nameof(contactheader), "", "", true); }
Comments
Post a Comment