WPF 自定义NotifyPropertyChanged
该工具类实现INotifyPropertyChanged接口
/// <summary> /// 实现了属性更改通知的基类 /// </summary> public class BaseNotifyPropertyChanged : System.ComponentModel.INotifyPropertyChanged { /// <summary> /// 属性值变化时发生 /// </summary> /// <param name="propertyName"></param> protected virtual void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName)); } /// <summary> /// 属性值变化时发生 /// </summary> /// <param name="propertyName"></param> protected virtual void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression) { var propertyName = (propertyExpression.Body as MemberExpression).Member.Name; this.OnPropertyChanged(propertyName); } public virtual event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; }
使用时继承此类,然后在VM中的属性get使用:OnPropertyChanged(()=>PropertyName)