WPF 实现INotifyPropertyChanged .Net Framework 4.5

  自己动手写了一个基类来实现INotifyPropertyChanged接口,以后可以直接使用。

       

 1 using System.ComponentModel;
 2 using System.Runtime.CompilerServices;
 3 
 4 public abstract class NotifyPropertyBase: INotifyPropertyChanged
 5 {
 6      public event PropertyChangedEventHandler PropertyChenged;
 7 
 8      protected void SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
 9      {
10           if (object.Equals(storage, value)) return;
11           storage = value;
12           this.OnPropertyChanged(propertyName);
13      }
14 
15      protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
16     {
17         if (this.PropertyChanged != null)
18             {
19                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
20              }
21     }
22 }

 

posted @ 2018-01-15 18:58  Johar  阅读(181)  评论(0编辑  收藏  举报