Cómo: Implementar la notificación de cambio de propiedadHow to: Implement Property Change Notification

Para admitir OneWay o TwoWay enlace para permitir que las propiedades de destino de enlace reflejen automáticamente los cambios dinámicos del origen de enlace (por ejemplo, para que el panel de vista previa se actualice automáticamente cuando el usuario edita un formulario), la clase debe proporcione las notificaciones de cambio de propiedad correctas.To support OneWay or TwoWay binding to enable your binding target properties to automatically reflect the dynamic changes of the binding source (for example, to have the preview pane updated automatically when the user edits a form), your class needs to provide the proper property changed notifications. En este ejemplo se muestra cómo crear una clase que implementa INotifyPropertyChanged.This example shows how to create a class that implements INotifyPropertyChanged.

EjemploExample

Para implementar INotifyPropertyChanged debe declarar el evento PropertyChanged y crear el método OnPropertyChanged.To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Por lo tanto, para cada propiedad para las que desee cambiar las notificaciones, calle a OnPropertyChanged cuando se actualice la propiedad.Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

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

Para ver un ejemplo de cómo se puede usar la clase Person para admitir TwoWay enlace, vea control cuando el texto del cuadro de texto actualiza el origen.To see an example of how the Person class can be used to support TwoWay binding, see Control When the TextBox Text Updates the Source.

Vea tambiénSee also