首页 > 代码库 > WPF后台通知前台事件(ViewModelBase记录)
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记录)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。