Share via


Procedura: implementare notifiche di modifiche alle proprietà

Aggiornamento: novembre 2007

Per supportare l'associazione OneWay o TwoWay in modo che le proprietà della destinazione dell'associazione riflettano automaticamente le modifiche dinamiche dell'origine dell'associazione (ad esempio per indurre l'aggiornamento automatico del riquadro anteprima quando un utente modifica un form), la classe deve fornire le notifiche di proprietà modificata appropriate. In questo esempio viene illustrato come creare una classe che implementa INotifyPropertyChanged.

Esempio

Per implementare INotifyPropertyChanged, è necessario dichiarare l'evento PropertyChanged e creare il metodo OnPropertyChanged. Per ogni proprietà per la quale si desidera fornire una notifica di modifica, chiamare OnPropertyChanged ogni volta che la proprietà viene aggiornata.

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));
          }
      }
  }
}

Per un esempio di utilizzo della classe Person per supportare l'associazione TwoWay, vedere Procedura: controllare il momento in cui il database di origine viene aggiornato dal testo di TextBox.

Per l'esempio completo, vedere Esempio di associazione semplice.

Vedere anche

Concetti

Cenni preliminari sulle origini di associazione

Cenni preliminari sull'associazione dati

Altre risorse

Esempi di associazione dati

Procedure relative all'associazione dati