WPF 中 MVVM 模式中最重要的两个类(内容来源于网络)

DelegateCommand.cs

  public class DelegateCommand : ICommand
    {
        Func<object, bool> canExecute;
        Action<object> executeAction;
        bool canExecuteCache;

        public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
        {
            this.executeAction = executeAction;
            this.canExecute = canExecute;
        }
        public DelegateCommand(Action<object> executeAction)
        {
            this.executeAction = executeAction;
            this.canExecute = CanExecute1;
        }

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            bool temp = canExecute(parameter);

            if (canExecuteCache != temp)
            {
                canExecuteCache = temp;
                if (CanExecuteChanged != null)
                {
                    CanExecuteChanged(this, new EventArgs());
                }
            }

            return canExecuteCache;
        }

        public bool CanExecute1(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            executeAction(parameter);
        }

        #endregion
    }

ViewModelBase.cs

 public class ViewModelBase : INotifyPropertyChanged,IDisposable
    {
        #region Public Properties  
        /// <summary>  
        /// 显示名称  
        /// </summary>  
        public virtual string DisplayName { get; protected set; }
        #endregion
        #region Constructor  
        /// <summary>  
        /// 实例化一个ViewModelBase对象  
        /// </summary>  
        protected ViewModelBase()
        {
        }
        #endregion
        #region INotifyPropertyChanged Members  
        /// <summary>  
        /// 触发属性发生变更事件  
        /// </summary>  
        /// <typeparam name="T">泛型标记,会匹配函数返回类型,不必手动填写</typeparam>  
        /// <param name="action">以函数表达式方式传入属性名称,表达式如下即可:()=>YourViewModelProperty</param>  
        protected void OnPropertyChanged<T>(Expression<Func<T>> action)
        {
            var propertyName = GetPropertyName(action);
            OnPropertyChanged(propertyName);
        }
        private static string GetPropertyName<T>(Expression<Func<T>> action)
        {
            var expression = (MemberExpression)action.Body;
            var propertyName = expression.Member.Name;
            return propertyName;
        }
        public void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
        #region IDisposable Members  
        public void Dispose()
        {
            this.OnDispose();
        }
        /// <summary>  
        /// 若支持IDisposable,请重写此方法,当被调用Dispose时会执行此方法。  
        /// </summary>  
        protected virtual void OnDispose()
        {
        }
        #endregion
    }

OnPropertyChanged()要在ViewModel的需要实时更新的属性的set中调用

展开阅读全文

没有更多推荐了,返回首页