WPF后台通知前台事件(ViewModelBase记录)
Posted xiao贝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF后台通知前台事件(ViewModelBase记录)相关的知识,希望对你有一定的参考价值。
版本1:
定义:
public class ModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } 引用: public class Person : ViewModelBase { private string _Name; /// <summary> /// 名字 /// </summary> public string Name { get { return _Name; } set { _Name = value; RaisePropertyChanged("Name"); } } }
版本2:
定义: public class ViewModelBase : INotifyPropertyChanged { protected void RaisePropertyChanged<T>(Expression<Func<T>> action) { var propertyName = GetPropertyName(action); RaisePropertyChanged(propertyName); } private static string GetPropertyName<T>(Expression<Func<T>> action) { var expression = (MemberExpression)action.Body; var propertyName = expression.Member.Name; return propertyName; } public void RaisePropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public event PropertyChangedEventHandler PropertyChanged; } 引用: public class Person : ViewModelBase { private string _Name; /// <summary> /// 名字 /// </summary> public string Name { get { return _Name; } set { _Name = value; RaisePropertyChanged(() => Name); } } }
以上是关于WPF后台通知前台事件(ViewModelBase记录)的主要内容,如果未能解决你的问题,请参考以下文章
如何将 WPF 按钮绑定到 ViewModelBase 中的命令?