INotifyPropertyChanged.PropertyChanged 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
發生於屬性值變更時。
// Register
event_token PropertyChanged(PropertyChangedEventHandler const& handler) const;
// Revoke with event_token
void PropertyChanged(event_token const* cookie) const;
// Revoke with event_revoker
INotifyPropertyChanged::PropertyChanged_revoker PropertyChanged(auto_revoke_t, PropertyChangedEventHandler const& handler) const;
event PropertyChangedEventHandler PropertyChanged;
function onPropertyChanged(eventArgs) { /* Your code */ }
iNotifyPropertyChanged.addEventListener("propertychanged", onPropertyChanged);
iNotifyPropertyChanged.removeEventListener("propertychanged", onPropertyChanged);
- or -
iNotifyPropertyChanged.onpropertychanged = onPropertyChanged;
Event PropertyChanged As PropertyChangedEventHandler
事件類型
範例
此範例示範如何實作 INotifyPropertyChanged 介面,並在屬性值改變時觸發 PropertyChanged 事件。 完整程式碼列表請參閱 XAML 資料綁定範例。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataBinding
{
public class Employee : INotifyPropertyChanged
{
private string _name;
private string _organization;
public string Name
{
get { return _name; }
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
public string Organization
{
get { return _organization; }
set
{
_organization = value;
RaisePropertyChanged("Organization");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
}
備註
使用 Microsoft .NET Framework 建置 UWP 應用程式時,此介面是隱藏的,開發者應使用 System.ComponentModel.INotifyPropertyChanged 介面。
PropertyChanged 事件可以透過 PropertyChangedEventArgs 的 PropertyName 屬性使用 String.Empty 表示物件上的所有屬性都已更改。 請注意,你不能像在 Windows Presentation Foundation(WPF)和 Microsoft Silverlight 那樣使用 null 來達成此功能。