ITPub博客

首页 > > > wpf MVVM疑问 求帮忙解析 谢谢!

wpf MVVM疑问 求帮忙解析 谢谢!

作者:R-B 时间:2019-09-12 11:43:12 0 删除 编辑

初学者 希望能通俗一点解析 谢谢!

代码:

viewModel 继承了NotificationObject 这个就是 

当属性值set 执行一个方法 RaisePropertyChanged  叫依赖属性。

 

同理需要一个命令属性 什么一个委托和委托对应的方法

class MainWindowViewModel : NotificationObject
    {
        private double input1;

        public double Input1
        {
            get { return input1; }
            set
            {
                input1 = value;
                this.RaisePropertyChanged("Input1");
            }
        }

        private double input2;

        public double Input2
        {
            get { return input2; }
            set
            {
                input2 = value;
                this.RaisePropertyChanged("Input2");
            }
        }

        private double result;

        public double Result
        {
            get { return result; }
            set
            {
                result = value;
                this.RaisePropertyChanged("Result");
            }
        }

        public DelegateCommand AddCommand { get; set; }private void Add(object parameter)
        {
            this.Result = this.Input1 + this.Input2;
        }
public MainWindowViewModel()
        {
            this.AddCommand = new DelegateCommand();
            this.AddCommand.ExecuteAction = new Action<object>(this.Add);
        }

ICommand是定义一个命令 下面有2个方法和一个事件

class DelegateCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            if (this.CanExecuteFunc == null)
            {
                return true;
            }

            return this.CanExecuteFunc(parameter);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (this.ExecuteAction == null)
            {
                return;
            }
            this.ExecuteAction(parameter);
        }

        public Action<object> ExecuteAction { get; set; }
        public Func<object, bool> CanExecuteFunc { get; set; }
    }

html绑定  <Button  Command="{Binding AddCommand}"/>

问题 Add是怎么和DelegateCommand关联起来的?

上面的代码是ok 的 。关联后怎么就执行了

Execute 里面的ExecuteAction ?

难以理解 求大侠和各位朋友解析下

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4560/viewspace-3120890/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: DIV隐藏。
请登录后发表评论 登录
全部评论

注册时间:2019-09-03

  • 博文量
    183
  • 访问量
    10151