INotifyPropertyChanged範例

  • 3205
  • 0

摘要:INotifyPropertyChanged範例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace INotifyPropertyChanged範例
{
    public class VM:INotifyPropertyChanged
    {
        //首先,定義PropertyChanged event
        public event PropertyChangedEventHandler PropertyChanged;

        //第二步,建立Method來發動事件
        protected void OnPropertyChanged(string prop)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }

        
        public VM()
        { 
        }

        //最後,當屬性改變時,呼叫 OnPropertyChanged
        private string name;
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                //在設定 name 值後,要呼叫 OnPropertyChanged 通知 view 此屬性已變更
                OnPropertyChanged("Name");
            }
        }
    }
}