Como: Implement Property Change Notification

Para dar suporte a associações OneWay ou TwoWay para habilitar suas propriedades alvo da associação a refletir automaticamente alterações dinâmicas da fonte da associação (por exemplo, para ter um painel de visualização atualizado automaticamente quando o usuário edita um formulário), sua classe precisa oferecer as notificações de alteração de propriedade adequadas. Este exemplo mostra como criar uma classe que implementa INotifyPropertyChanged.

Exemplo

Para implementar INotifyPropertyChanged você precisa declarar o evento PropertyChanged e criar o método OnPropertyChanged. Depois para cada propriedade que você quiser notificações de alterações, chame OnPropertyChanged sempre que a propriedade for atualizada.

Imports System.ComponentModel

' This class implements INotifyPropertyChanged
' to support one-way and two-way bindings
' (such that the UI element updates when the source
' has been changed dynamically)
Public Class Person
    Implements INotifyPropertyChanged

    Private personName As String

    Sub New()
    End Sub

    Sub New(ByVal Name As String)
        Me.personName = Name
    End Sub

    ' Declare the event
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public Property Name() As String
        Get
            Return personName
        End Get
        Set(ByVal value As String)
            personName = value
            ' Call OnPropertyChanged whenever the property is updated
            OnPropertyChanged("Name")
        End Set
    End Property

    ' Create the OnPropertyChanged method to raise the event
    Protected Sub OnPropertyChanged(ByVal name As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
    End Sub

End Class
using System.ComponentModel;

namespace SDKSample
{
  // This class implements INotifyPropertyChanged
  // to support one-way and two-way bindings
  // (such that the UI element updates when the source
  // has been changed dynamically)
  public class Person : INotifyPropertyChanged
  {
      private string name;
      // Declare the event
      public event PropertyChangedEventHandler PropertyChanged;

      public Person()
      {
      }

      public Person(string value)
      {
          this.name = value;
      }

      public string PersonName
      {
          get { return name; }
          set
          {
              name = value;
              // Call OnPropertyChanged whenever the property is updated
              OnPropertyChanged("PersonName");
          }
      }

      // Create the OnPropertyChanged method to raise the event
      protected void OnPropertyChanged(string name)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(name));
          }
      }
  }
}

Para ver um exemplo de como a classe Person pode ser usada para suportar associação TwoWay, veja Como: Control When the TextBox Text Updates the Source.

For the complete sample, see Exemplo de ligação simples.

Consulte também

Conceitos

Visão geral sobre associação de fontes

Revisão de Associação de Dados

Outros recursos

Exemplos de ligação de dados

Data Binding How-to Topics