Gewusst wie: Implementieren von Benachrichtigungen bei EigenschaftenänderungenHow to: Implement Property Change Notification

Um die OneWay TwoWay dynamischen Änderungen der Bindungs Quelle automatisch widerzuspiegeln (z. b. wenn der Vorschaubereich automatisch aktualisiert werden soll, wenn der Benutzer ein Formular bearbeitet), muss die Klasse die richtigen Benachrichtigungen über geänderte Eigenschaften bereitstellen, damit die Bindungs Zieleigenschaften die dynamischen Änderungen der Bindungs Quelle automatisch widerspiegeln.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. Dieses Beispiel zeigt, wie eine Klasse erstellt wird, die implementiert INotifyPropertyChanged .This example shows how to create a class that implements INotifyPropertyChanged.

BeispielExample

Zum Implementieren von INotifyPropertyChanged müssen Sie das PropertyChanged -Ereignis deklarieren und die-Methode erstellen OnPropertyChanged .To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Dann rufen Sie für jede Eigenschaft, für die Sie Änderungsbenachrichtigungen erhalten möchten, OnPropertyChanged auf, wenn die Eigenschaft aktualisiert wird.Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

using System.ComponentModel;
using System.Runtime.CompilerServices;

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

      // Create the OnPropertyChanged method to raise the event
      // The calling member's name will be used as the parameter.
      protected void OnPropertyChanged([CallerMemberName] string name = null)
      {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
      }
  }
}
Imports System.ComponentModel
Imports System.Runtime.CompilerServices

' 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()
        End Set
    End Property

    ' Create the OnPropertyChanged method to raise the event
    ' Use the name of the member that called this method in place of name
    Protected Sub OnPropertyChanged(<CallerMemberName> Optional name As String = Nothing)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
    End Sub

End Class

Ein Beispiel für die Verwendung der- Person Klasse zur Unterstützung von Bindungen finden Sie unter TwoWay Steuern, wann der TextBox-Text die Quelle aktualisiert.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.

Weitere InformationenSee also