首页 新闻 会员 周边

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

0
悬赏园豆:5 [已解决问题] 解决于 2014-05-14 14:38

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

代码:

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 ?

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

s_p的主页 s_p | 初学一级 | 园豆:138
提问于:2014-05-08 09:44
< >
分享
最佳答案
0

<Button  Command="{Binding AddCommand}"/>

编译器会帮你解析此条脚本,生成对应的托管代码。

收获园豆:2
Launcher | 高人七级 |园豆:45045 | 2014-05-08 10:57

 我后面明白了 调试 

s_p | 园豆:138 (初学一级) | 2014-05-14 14:39
其他回答(2)
0

this.AddCommand = new DelegateCommand();

this.AddCommand.ExecuteAction = new Action<object>(this.Add);

就是这样关联的啊兄弟

收获园豆:2
YoMe | 园豆:545 (小虾三级) | 2014-05-08 21:40

 我后面明白了 调试

支持(0) 反对(0) s_p | 园豆:138 (初学一级) | 2014-05-14 14:39
0

首先要理解DataContext属性和INotifyPropertyChanged接口的原理。直接接触MVVM肯定是不行的。

收获园豆:1
h82258652 | 园豆:293 (菜鸟二级) | 2014-05-13 15:31

 你说的对

支持(0) 反对(0) s_p | 园豆:138 (初学一级) | 2014-05-14 14:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册