INotifyPropertyChanged 인터페이스는 속성 값이 변경되었음을 클라이언트(대개 바인딩하는 클라이언트)에 알리는 데 사용됩니다.
예를 들어, FirstName이라는 속성이 있는 Person 개체의 경우 제네릭 속성 변경 알림을 제공하기 위해 Person 형식은 INotifyPropertyChanged 인터페이스를 구현하고 FirstName이 변경될 때 PropertyChanged 이벤트를 발생시킵니다.
다음 코드 예제에서는 INotifyPropertyChanged 인터페이스를 구현하는 방법을 보여 줍니다.
//Add using statements
using System.ComponentModel;
using System.Windows.Data;
...
// Create a class that implements INotifyPropertyChanged
public class Person : INotifyPropertyChanged
{
private string firstNameValue;
public string FirstName{
get { return firstNameValue; }
set
{
firstNameValue=value;
// Call NotifyPropertyChanged when the property is updated
NotifyPropertyChanged("FirstName");
}
}
// Declare the PropertyChanged event
public event PropertyChangedEventHandler PropertyChanged;
// NotifyPropertyChanged will raise the PropertyChanged event passing the
// source property that is being updated.
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
또다른 예제
// Create a class that implements INotifyPropertyChanged.
public class MyColors : INotifyPropertyChanged
{
private SolidColorBrush _Brush1;
// Declare the PropertyChanged event.
public event PropertyChangedEventHandler PropertyChanged;
// Create the property that will be the source of the binding.
public SolidColorBrush Brush1
{
get { return _Brush1; }
set
{
_Brush1 = value;
// Call NotifyPropertyChanged when the source property is updated.
NotifyPropertyChanged("Brush1");
}
}
// NotifyPropertyChanged will raise the PropertyChanged event, passing
// the source property that is being updated.
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}'WPF' 카테고리의 다른 글
| 컨테이너(Container) (0) | 2009.06.03 |
|---|---|
| 모듈화 (0) | 2009.06.03 |
| XML , RSS 기사받아오기 및 웹서비스로 다시 보내주기 (0) | 2009.05.29 |
| 실버라이트 초간단 ADO.NET Entity Data Model 만들기 (0) | 2009.05.29 |
| IValueConverter 데이터 변환 (0) | 2009.05.28 |
| 델리게이트(delegate)로 유저컨트롤 이벤트 케치하기 (0) | 2009.05.28 |
| 초간단 컬렉션에 바인딩하고 마스터/세부 뷰 만들기 (0) | 2009.05.28 |
| Silverlight SDK Samples (0) | 2009.05.28 |
| INotifyPropertyChanged (0) | 2009.05.28 |
| Silverlight 2를 사용하여 데이터 중심 웹 응용 프로그램 만들기 (0) | 2009.05.28 |