방법: INotifyPropertyChanged 인터페이스 구현

다음 코드 예제에서는 INotifyPropertyChanged 인터페이스를 구현하는 방법을 보여 줍니다. 이 인터페이스는 Windows Forms 데이터 바인딩에 사용되는 비즈니스 개체에 대해 구현합니다. 이 인터페이스를 구현하면 비즈니스 개체에 대한 속성 변경 사항이 바인딩된 컨트롤에 통보됩니다.

예제

' This class implements a simple customer type 
' that implements the IPropertyChange interface.

Public Class DemoCustomer
    Implements INotifyPropertyChanged

    ' These fields hold the values for the public properties.
    Private idValue As Guid = Guid.NewGuid()
    Private customerName As String = String.Empty
    Private companyNameValue As String = String.Empty
    Private phoneNumberValue As String = String.Empty

    Public Event PropertyChanged As PropertyChangedEventHandler _
      Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub


    ' The constructor is private to enforce the factory pattern.
    Private Sub New() 
        customerName = "no data"
        companyNameValue = "no data"
        phoneNumberValue = "no data"

    End Sub 'New


    ' This is the public factory method.
    Public Shared Function CreateNewCustomer() As DemoCustomer 
        Return New DemoCustomer()

    End Function

    ' This property represents an ID, suitable
    ' for use as a primary key in a database.
    Public ReadOnly Property ID() As Guid
        Get
            Return Me.idValue
        End Get
    End Property


    Public Property CompanyName() As String 
        Get
            Return Me.companyNameValue
        End Get 
        Set
            If value <> Me.companyNameValue Then
                Me.companyNameValue = value
                NotifyPropertyChanged("CompanyName")
            End If
        End Set
    End Property

    Public Property PhoneNumber() As String 
        Get
            Return Me.phoneNumberValue
        End Get 
        Set
            If value <> Me.phoneNumberValue Then
                Me.phoneNumberValue = value
                NotifyPropertyChanged("PhoneNumber")
            End If
        End Set
    End Property
End Class
// This class implements a simple customer type 
// that implements the IPropertyChange interface.
public class DemoCustomer : INotifyPropertyChanged
{
    // These fields hold the values for the public properties.
    private Guid idValue = Guid.NewGuid();
    private string customerName = String.Empty;
    private string companyNameValue = String.Empty;
    private string phoneNumberValue = String.Empty;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    // The constructor is private to enforce the factory pattern.
    private DemoCustomer()
    {
        customerName = "no data";
        companyNameValue = "no data";
        phoneNumberValue = "no data";
    }

    // This is the public factory method.
    public static DemoCustomer CreateNewCustomer()
    {
        return new DemoCustomer();
    }

    // This property represents an ID, suitable
    // for use as a primary key in a database.
    public Guid ID
    {
        get
        {
            return this.idValue;
        }
    }

    public string CompanyName
    {
        get {return this.companyNameValue;}

        set
        {
            if (value != this.companyNameValue)
            {
                this.companyNameValue = value;
                NotifyPropertyChanged("CompanyName");
            }
        }
    }
    public string PhoneNumber
    {
        get { return this.phoneNumberValue; }

        set 
        {
            if (value != this.phoneNumberValue)
            {
                this.phoneNumberValue = value;
                NotifyPropertyChanged("PhoneNumber");
            }
        }
    }
}

코드 컴파일

위의 코드 예제를 컴파일하려면

  • 코드를 빈 코드 파일에 붙여넣습니다. Main 메서드가 포함된 Windows Forms 응용 프로그램에 이 비즈니스 개체를 사용합니다.

참고 항목

작업

방법: PropertyNameChanged 패턴 적용

방법: BindingSource와 INotifyPropertyChanged 인터페이스를 사용하여 변경 내용 알림 발생

개념

Windows Forms 데이터 바인딩의 변경 알림

기타 리소스

Windows Forms 데이터 바인딩