我必须查看ViewModels, OrganizationContact和PersonalInformationModel。
OrganizationContact使用PersonalInformationModel。
它们是这样设置的:
OrganizationContact:
public class OrganizationContact : ViewModelBase
{
private PersonalInformationModel _contactInfo;
public PersonalInformationModel ContactInfo
{
get
{
return _contactInfo;
}
set
{
_contactInfo = value;
RaisePropertyChanged(nameof(ContactHeader), "", "", true);
RaisePropertyChanged(nameof(ContactInfo), null, _contactInfo, true);
}
}
//Generate Header
public string ContactHeader
{
get
{
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
{
get
{
return _firstName;
}
set
{
_firstName = value;
RaisePropertyChanged(nameof(FirstName), "", _firstName, true);
}
}
public string MiddleInitial
{
get
{
return _middleInitial;
}
set
{
_middleInitial= value;
RaisePropertyChanged(nameof(MiddleInitial),"",_middleInitial,true);
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
RaisePropertyChanged(nameof(LastName), "", _lastName, true);
}
}
public string PhoneNumber
{
get
{
return _phoneNumber;
}
set
{
_phoneNumber = value;
RaisePropertyChanged(nameof(PhoneNumber), "", _phoneNumber, true);
}
}
public string PhoneExtension
{
get
{
return _phoneExtension;
}
set
{
_phoneExtension = value;
RaisePropertyChanged(nameof(PhoneExtension), "", _phoneExtension, true);
}
}
public string FaxNumber
{
get
{
return _faxNumber;
}
set
{
_faxNumber = value;
RaisePropertyChanged(nameof(FaxNumber), "", _faxNumber, true);
}
}
public string Email
{
get
{
return _email;
}
set
{
_email = value;
RaisePropertyChanged(nameof(Email),"",_email, true);
}
}
public string FullName => $"{FirstName} {LastName}";
}
PersonalInformationModel被其他类使用。
我正在寻找的是OrganizationContact
被告知的方法,如果PersonalInformationModel
内部的任何属性发生变化,那么OrganizationContact
内部的ContactHeader
可以被通知变化。
好的,为了得到你想要的,你需要做一些事情。首先,当您在OrganizationContact上设置ContactInfo属性时,注册PropertyChanged处理程序:
public PersonalInformationModel ContactInfo
{
get
{
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);
}
}
现在,创建处理程序。你应该能够在ContactHeader上引发PropertyChanged事件来更新你的绑定。
void ContactInfo_PropertyChanged(object sender, PropertyChangedEventArgs args)
{
RaisePropertyChanged(nameof(ContactHeader), "", "", true);
}