Observação
O acesso a essa página exige autorização. Você pode tentar entrar ou alterar diretórios.
O acesso a essa página exige autorização. Você pode tentar alterar os diretórios.
Para dar suporte à vinculação OneWay ou TwoWay, permitindo que as propriedades de destino da vinculação reflitam automaticamente as mudanças dinâmicas da fonte de vinculação (por exemplo, para atualizar automaticamente o painel de visualização quando o usuário edita um formulário), sua classe precisa fornecer as notificações de alteração das propriedades adequadas. Este exemplo mostra como criar uma classe que implementa INotifyPropertyChanged.
Exemplo
Para implementar INotifyPropertyChanged você precisa declarar o evento PropertyChanged e criar o método OnPropertyChanged
. Em seguida, para cada propriedade para a qual você deseja alterar as notificações, chame OnPropertyChanged
sempre que a propriedade for atualizada.
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
Para ver um exemplo de como a classe Person
pode ser usada para dar suporte à associação de TwoWay, consulte Controlar quando o texto do TextBox atualiza a origem.
Consulte também
- Visão geral das fontes de vinculação
- Visão Geral do Data Binding
- Tópicos "Como Fazer"
.NET Desktop feedback