共用方式為


INotifyPropertyChanged.PropertyChanged 事件

定義

當屬性值變更時發生。

// 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 事件可以指出物件上的所有屬性都已針對PropertyChangedEventArgsPropertyName屬性使用String.Empty變更。 請注意,您無法對這個使用null,就像在 WPF) 和 Microsoft Silverlight Windows Presentation Foundation (中一樣。

適用於

另請參閱