我从来没有工作wuth INotifyPropertyChanged。我正在阅读文章,并从那里找到与INotifyPropertyChanged相关的代码。关于INotifyPropertyChanged
public class ContactModel : INotifyPropertyChanged
{
private string _firstName;
public string FirstName
{
get { return _firstName; }
set
{
_firstName = value;
RaisePropertyChanged("FirstName");
RaisePropertyChanged("FullName");
}
}
private string _lastName;
public string LastName
{
get { return _lastName; }
set
{
_lastName = value;
RaisePropertyChanged("LastName");
RaisePropertyChanged("FullName");
}
}
public string FullName
{
get { return string.Format("{0} {1}", FirstName, LastName); }
}
protected void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public override bool Equals(object obj)
{
return obj is ContactModel && ((ContactModel) obj).FullName.Equals(FullName);
}
public override int GetHashCode()
{
return FullName.GetHashCode();
}
}
从上面的代码我发现RaisePropertyChanged(“FirstName”);总是从属性设置器中调用。为什么RaisePropertyChanged需要调用。我搜索谷歌良好的解释,但我没有找到任何好的链接。所以请如果有人解释为什么RaisePropertyChanged需要从setter调用。
感谢
你知道事件的一般工作原理吗? – 2011-02-10 12:48:02