首页 > Windows开发 > 详细

WPF 定义Command

时间:2017-09-15 01:37:15      阅读:424      评论:0      收藏:0      [点我收藏+]

直接上代码:

    public class LoginDelegateCommand : ICommand
    {
        private Action _execute;

        private Predicate<object> _canExecute;

        public LoginDelegateCommand([NotNull]Action execute): this(execute, DefaultCanExecute)
        {
        }

        public LoginDelegateCommand([NotNull]Action execute, Predicate<object> canExecute)
        {
            this._execute = execute ?? throw new ArgumentNullException("execute");
            this._canExecute = canExecute ?? throw new ArgumentNullException("canExecute");
        }
        public bool CanExecute(object parameter)
        {
            return this._canExecute != null && this._canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            UserLoginHelper.NotifyToLoginWindow(() =>
            {
                this._execute();
            });
        }
        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
                this.CanExecuteChangedInternal += value;
            }

            remove
            {
                CommandManager.RequerySuggested -= value;
                this.CanExecuteChangedInternal -= value;
            }
        }

        private event EventHandler CanExecuteChangedInternal;

        public void OnCanExecuteChanged()
        {
            EventHandler handler = this.CanExecuteChangedInternal;
            handler?.Invoke(this, EventArgs.Empty);
        }

        private static bool DefaultCanExecute(object parameter)
        {
            return true;
        }
    }

 

在viewmodel中,定义一个Command属性

Command=new LoginDelegateCommand (()={添加逻辑});

然后绑定即可。

WPF 定义Command

原文:http://www.cnblogs.com/kybs0/p/7523654.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!