Alle Silverlight-Beispiele mit MVVM verwenden die Schnittstelle IPropertyChanged. Was ist das Konzept dahinter und warum müssen wir ein Ereignis auslösen, wenn wir einen Wert setzen?
Beispiel: -
public class UserNPC:INotifyPropertyChanged
{
private string name;
public string Name {
get { return name; }
set { name = value; onPropertyChanged(this, "Name"); }
}
public int grade;
public int Grade {
get { return grade; }
set { grade = value; onPropertyChanged(this, "Grade"); }
}
// Declare the PropertyChanged event
public event PropertyChangedEventHandler PropertyChanged;
// OnPropertyChanged will raise the PropertyChanged event passing the
// source property that is being updated.
private void onPropertyChanged(object sender, string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}
}
Was ist der genaue Zweck von INotifyPropertyChanged?