码迷,mamicode.com
首页 > Windows程序 > 详细

WPF后台通知前台事件(ViewModelBase记录)

时间:2017-02-07 12:11:39      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:bsp   model   member   res   mode   value   action   cte   inotify   

版本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记录)

标签:bsp   model   member   res   mode   value   action   cte   inotify   

原文地址:http://www.cnblogs.com/zbfamily/p/6373408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!