美文网首页
C# WPF MVVM Command Binding

C# WPF MVVM Command Binding

作者: 不会旗子 | 来源:发表于2019-04-25 09:22 被阅读0次

    单窗口程序,程序执行顺序:

    //1.窗口初始化时,触发CanExecuteChanged事件的add方法
    //网上说这个事件是:  检查命令是否可以执行的事件,在UI事件发生导致控件状态或数据发生变化时触发
    public event EventHandler CanExecuteChanged
            {
                add
                {
                    if (_canExecute != null)
                    {
                        CommandManager.RequerySuggested += value;
                    }
                }
            }
    //2.一个控件触发add后,就会触发get方法,(窗口初始化时就要获得绑定对象?)
    public MyCommand ParamCommand
            {
                get
                {
                    if (_paramCommand == null)
                        _paramCommand = new MyCommand(
                            new Action<object>(
                                o => MessageBox.Show(o.ToString())),
                            new Func<object, bool>(
                                o => !string.IsNullOrEmpty(o.ToString())));
                    return _paramCommand;
                }
            }
    //3.一旦Command对象实例化完毕,就会不断地触发CanExecute委托,不带停的
    //现在还不知道为什么,感觉这个要少用
    public bool CanExecute(object parameter)
            {
                MessageBox.Show("默认的CanExecute");
                return true;
            }
    //4.其他控件的1,2,3方法
    //5.当所有控件加载完毕,CanExecute会不断触发,这时点击就会触发Exucute委托
    //6.当关闭程序时,会触发CanExucuteChanged的remove方法
    public event EventHandler CanExecuteChanged
            {
                remove
                {
                    if (_canExecute != null)
                    {
                        CommandManager.RequerySuggested -= value;
                    }
                }
            }
    

    相关文章

      网友评论

          本文标题:C# WPF MVVM Command Binding

          本文链接:https://www.haomeiwen.com/subject/nxzogqtx.html