zoukankan      html  css  js  c++  java
  • DelegateCommand.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Input;

    namespace SC
    {
        /// <summary>
        /// delegate command for view model
        /// </summary>
        public class DelegateCommand : ICommand
        {
            #region members
            /// <summary>
            /// can execute function
            /// </summary>
            private readonly Func<bool> canExecute;

            /// <summary>
            /// execute function
            /// </summary>
            private readonly Action execute;

            #endregion

            /// <summary>
            /// Initializes a new instance of the DelegateCommand class.
            /// </summary>
            /// <param name="execute">indicate an execute function</param>
            public DelegateCommand(Action execute)
                : this(execute, null)
            {
            }

            /// <summary>
            /// Initializes a new instance of the DelegateCommand class.
            /// </summary>
            /// <param name="execute">execute function </param>
            /// <param name="canExecute">can execute function</param>
            public DelegateCommand(Action execute, Func<bool> canExecute)
            {
                this.execute = execute;
                this.canExecute = canExecute;
            }

            /// <summary>
            /// can executes event handler
            /// </summary>
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }

            /// <summary>
            /// implement of icommand can execute method
            /// </summary>
            /// <param name="parameter">parameter by default of icomand interface</param>
            /// <returns>can execute or not</returns>
            public bool CanExecute(object parameter)
            {
                if (this.canExecute == null)
                {
                    return true;
                }

                return this.canExecute();
            }

            /// <summary>
            /// implement of icommand interface execute method
            /// </summary>
            /// <param name="parameter">parameter by default of icomand interface</param>
            public void Execute(object parameter)
            {
                this.execute();
            }
        }

        /// <summary>
        /// delegate command for view model
        /// </summary>
        public class DelegateCommand<T> : ICommand
        {
            #region members
            /// <summary>
            /// can execute function
            /// </summary>
            private readonly Func<T, bool> canExecute;

            /// <summary>
            /// execute function
            /// </summary>
            private readonly Action<T> execute;

            #endregion

            /// <summary>
            /// Initializes a new instance of the DelegateCommand class.
            /// </summary>
            /// <param name="execute">indicate an execute function</param>
            public DelegateCommand(Action<T> execute)
                : this(execute, null)
            {
            }

            /// <summary>
            /// Initializes a new instance of the DelegateCommand class.
            /// </summary>
            /// <param name="execute">execute function </param>
            /// <param name="canExecute">can execute function</param>
            public DelegateCommand(Action<T> execute, Func<T, bool> canExecute)
            {
                this.execute = execute;
                this.canExecute = canExecute;
            }

            /// <summary>
            /// can executes event handler
            /// </summary>
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }

            /// <summary>
            /// implement of icommand can execute method
            /// </summary>
            /// <param name="parameter">parameter by default of icomand interface</param>
            /// <returns>can execute or not</returns>
            public bool CanExecute(object parameter)
            {
                if (this.canExecute == null)
                {
                    return true;
                }

                return this.canExecute((T)parameter);
            }

            /// <summary>
            /// implement of icommand interface execute method
            /// </summary>
            /// <param name="parameter">parameter by default of icomand interface</param>
            public void Execute(object parameter)
            {
                this.execute((T)parameter);
            }
        }
    }

  • 相关阅读:
    计算几何学习8
    c语言数据结构学习心得——队列
    c语言数据结构学习心得——栈
    c语言数据结构学习心得——数据结构基本概念
    c语言数据结构学习心得——图
    c语言数据结构学习心得——树
    c语言数据结构学习心得——二叉树
    c语言数据结构学习心得——线性表
    Asp.net 2.0 Webpart 数据库的迁移
    BUGReport:datagrid带模板列绑定空数据集出错的问题
  • 原文地址:https://www.cnblogs.com/zisezhixin/p/3971961.html
Copyright © 2011-2022 走看看