Reputation: 34188
i never work wuth INotifyPropertyChanged. i was reading article and from there i found code related with 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();
}
}
from the above code i found RaisePropertyChanged("FirstName"); is always called from property setter. why RaisePropertyChanged is required to call. i search google for good explaination but i did not found any good link. so please if someone explain why RaisePropertyChanged need to call from setter.
thanks
Upvotes: 0
Views: 351
Reputation: 14387
You should check inside the property setter if the property actually changes, then by rasing the event you can notify that your property has changed:
set
{
if (lastname != value)
{
_lastName = value;
RaisePropertyChanged("LastName");
RaisePropertyChanged("FullName");
}
}
Any class that has subscribed to the PropertyChanged event, will then be notified that the property has changed, so they can update their bindings.
Upvotes: 2
Reputation: 3374
This is so all subscribing recipients get a message stating that the property has changed, therefore they should update their values accordingly. For example, a grid will change text.
The reason they've updated the FullName
property as well, is because the First/Last name properties directly affect the FullName of the person, therefore bindings should reflect these values.
Upvotes: 1
Reputation: 81660
Because WPF registers to the PropertyChanged
event and this will make the binding work, i.e. you change the property and textbox text changes.
Also the event raising pattern is a standard one: Event is raised through a protected method so that the subclasses can turn it off/or change the behaviour.
I actually have a bit more on my view models that verify the name - originally by Josh Smith:
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
VerifyPropertyName(propertyName);
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
protected void OnPropertyChanged(object sender, string propertyName)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
public void VerifyPropertyName(string propertyName)
{
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
{
throw new Exception("Invalid property name: " + propertyName);
}
}
Upvotes: 3