كيفية القيام بما يلي: تطبيق إعلام تغيير الخاصية
لدعم ربط OneWay أو TwoWay لتمكين خصائص هدف الربط الخاص بك من عكس تلقائياً التغيير الحيوي لمصدر الربط (على سبيل المثال، ليتم تحديث جزء المعاينة تلقائياً عندما يقوم المستخدم بتحرير نموذج)، تحتاج فئتك إلى توفير إعلامات تغيير الخاصية المناسبة. يوضح هذا المثال كيفية إنشاء الفئة التي تطبق INotifyPropertyChanged.
مثال
لتطبيق INotifyPropertyChanged تحتاج إلي تعريف حدث PropertyChanged و إنشاء أسلوب OnPropertyChanged. ثم لكل خاصية تريد تغيير إعلاماتها قم باستدعاء OnPropertyChanged كلما يتم تحديث الخاصية.
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));
}
}
}
}
لمشاهدة مثال على كيفية استخدام فئة Person لدعم ربط TwoWay، راجع كيفية القيام بما يلي: التحكم عندما يحدث نص مربع النص المصدر.