Nasıl yapılır: Özellik Değişikliği Bildirimi Uygulama

Bağlama OneWayTwoWay hedefi özelliklerinizi bağlama kaynağının dinamik değişikliklerini otomatik olarak yansıtacak şekilde etkinleştirmek için (örneğin, Kullanıcı bir formu düzenlediğinde Önizleme bölmesinin otomatik olarak güncelleştirilmesini sağlamak için), sınıfınızın doğru özellik değişmiş bildirimleri sağlaması gerekir. Bu örnek, uygulayan bir sınıfın nasıl oluşturulacağını gösterir INotifyPropertyChanged .

Örnek

Uygulamak için, INotifyPropertyChangedPropertyChanged olayı bildirmeniz ve metodunu oluşturmanız gerekir OnPropertyChanged . Ardından, değişiklik bildirimlerini istediğiniz her özellik için, OnPropertyChanged özelliği her güncelleştirildiğinde çağırın.

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

Sınıfın bağlamayı desteklemek için nasıl kullanılabileceği hakkında bir örnek görmek için PersonTwoWay , bkz. Person.

Ayrıca bkz.