wpf自定义Mvvm框架

1.DelegateCommand.cs

  public  class DelegateCommand : ICommand
        {
            public Action ExecuteAction { get; set; }
            public Func CanExecuteFunc { get; set; }
            public event EventHandler CanExecuteChanged;
            public bool CanExecute(object parameter)
            {
                // throw new NotImplementedException();
                if (this.CanExecuteFunc == null)
                {
                    return true;
                }
                this.CanExecuteFunc(parameter);
                return true;
            }

            public void Execute(object parameter)
            {
                //throw new NotImplementedException();
                if (this.ExecuteAction == null)
                {
                    return;
                }
                this.ExecuteAction(parameter); //命令->Execute->Execute指向的方法
            }

            public DelegateCommand()  {  }
            public DelegateCommand(Action execute):this(execute,null) { }

            public DelegateCommand(Action execute, Func canexcute) 
            {
                this.CanExecuteFunc = canexcute;
                this.ExecuteAction = execute;
            } 

        } 
  

NotificationObject.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleMvvmDemo.viewmodel
{
    //viewmodel的基类
    class NotificationObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if(this.PropertyChanged!=null)
            {
                //binding监控changed
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

你可能感兴趣的:(wpf)