속성이 변경되었을 때 클래스가 클라이언트에게 알려야 하는 경우 이 인터페이스를 구현한다.
인터페이스에는 속성이 변경될 때 발생해야 하는 PropertyChanged 이벤트의 구현이 필요하다.
public class MyObservable : INotifyPropertyChanged
{
private int value;
public event PropertyChangedEventHandler PropertyChanged;
public int Value
{
get { return value; }
set
{
if (this.value != value)
{
this.value = value;
OnPropertyChanged("Value");
}
}
}
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
이 예제에서 MyObservable 클래스는 INotifyPropertyChanged 인터페이스를 구현하여 속성이 변경
될 때 클라이언트에게 알립니다.
Value 속성은 값이 변경될 때 PropertyChanged 이벤트를 발생시킵니다.