Guide pratique pour implémenter une notification de modification de propriété
Pour prendre en charge les liaisons OneWay ou TwoWay afin de permettre à vos propriétés cibles de la liaison de refléter automatiquement les modifications dynamiques de la source de liaison (par exemple, pour que le volet d’aperçu soit mis à jour automatiquement lorsque l’utilisateur modifie un formulaire), votre classe doit fournir les notifications appropriées de modification de propriété. Cet exemple montre comment créer une classe qui implémente INotifyPropertyChanged.
Exemple
Pour implémenter INotifyPropertyChanged vous devez déclarer l’événement PropertyChanged et créer la méthode OnPropertyChanged
. Ensuite, pour chaque propriété pour laquelle vous souhaitez recevoir des notifications de modification, vous appelez OnPropertyChanged
chaque fois que la propriété est mise à jour.
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
Pour voir un exemple de la manière d’utiliser la classe Person
pour prendre en charge la liaison TwoWay, consultez la page Contrôler quand le texte TextBox met à jour la source.
Voir aussi
.NET Desktop feedback