共用方式為


HOW TO:實作 INotifyPropertyChanged 介面

下列程式碼範例將示範如何實作 INotifyPropertyChanged 介面。 在 Windows Form 資料繫結中所使用的商務物件 (Business Object) 中實作此介面。 實作時,介面會通訊至商務物件上其屬性變更的繫結控制項。

範例

' 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 Form 應用程式中的商務物件。

請參閱

工作

HOW TO:套用 PropertyNameChanged 模式

HOW TO:使用 BindingSource 和 INotifyPropertyChanged 介面引發變更告知

概念

Windows Form 資料繫結中的變更告知

其他資源

Windows Form 資料繫結