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);
        }
    }
}

DelegateCommand.cs的更多相关文章

  1. WPF的MVVM

    一.关于WPF WPF(Windows  Presentation Foundation) ,从名字来看,Microsoft想把WPF技术作为Windows程序外观(表现层)的基础.我们知道,现在开发 ...

  2. Enterprise Library 6.0 参考源码索引

    http://www.projky.com/entlib/6.0/Diagnostics/Tracing/DiaLib.cs.htmlhttp://www.projky.com/entlib/6.0/ ...

  3. Enterprise Library 5.0 参考源码索引

    http://www.projky.com/entlib/5.0/Microsoft/Practices/EnterpriseLibrary/Caching/BackgroundScheduler.c ...

  4. WPF比较两个随机数大小写,利用MVVM思想实现

    MVVM模式是把表现层和业务层完全分离,所以这里就使用MVVM制作一个极其简单的WPF的例子: 先看看最终图:

  5. WPF MVVM+EF 增删改查 简单示例(一)

    实现了那些功能,先看看效果图: 项目工程目录: 接下来开始具体的步骤: 第一步:在VS中新建工程 第二步:使用NuGet 安装EntityFramework 第三步:使用NuGet 安装EntityF ...

  6. WPF+MVVM+EF示例1

    实现了那些功能,先看看效果图: 项目工程目录: 接下来开始具体的步骤: 第一步:在VS中新建工程 第二步:使用NuGet 安装EntityFramework 第三步:使用NuGet 安装EntityF ...

  7. WPF MVVM实例三

    在没给大家讲解wpf mwm示例之前先给大家简单说下MVVM理论知识: WPF技术的主要特点是数据驱动UI,所以在使用WPF技术开发的过程中是以数据为核心的,WPF提供了数据绑定机制,当数据发生变化时 ...

  8. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  9. Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结

    Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结 1.1. 软件体系架构是沿着单机到 CS 架构,再到 BS 的三层架构甚至多层架构逐步发展过来的,关于 ...

随机推荐

  1. Java基础之泛型——使用通配符类型参数(TryWildCard)

    控制台程序 使用通配符类型参数可以设定方法的参数类型,其中的代码对于泛型类的实际类型参数不能有任何依赖.如果将方法的参数类型设定为Binary<?>,那么方法可以接受BinaryTree& ...

  2. Azure billing 分析(2)

    美国中南部的2008R2的A1的VM放了一天,CPU时间涨了13个小时,有点小贵,真的没有操作啊... 提示早上7到9点有一个小高峰. 看来平时没什么访问量时,还是改成A0能省点钱.因为第一天是用A0 ...

  3. 深入理解JAVA I/O系列六:Linux中的IO模型

    IO模型 linux系统IO分为内核准备数据和将数据从内核拷贝到用户空间两个阶段. 这张图大致描述了数据从外部磁盘向运行中程序的内存中移动的过程. 用户空间.内核空间 现在操作系统都是采用虚拟存储器, ...

  4. PostgreSQL simple select(group by and insert into ...select)

    warehouse_db=# create table student(number int primary key,name varchar(20),age int);CREATE TABLEwar ...

  5. BroadCast Receive 生命周期

    BroadCastReceiver 简介 BroadCastReceiver 源码位于: framework/base/core/java/android.content.BroadcastRecei ...

  6. windows namedPipe 命名管道clent and server

    1.client: #include "iostream" #include "windows.h" using namespace std; void mai ...

  7. struts文件上传(单文件)

    第01步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  8. 夺命雷公狗ThinkPHP项目之----企业网站12之文章添加的实现

    我们现在就开始写文章添加了,居然是添加当然布列外,我们还是要先讲模版搞定再说被: <!doctype html> <html> <head> <meta ch ...

  9. ssh & display

    在Windows下用ssh连接服务器的话putty是一个小巧而且实用的工具,如果想要图形界面,可以使用X工具配合putty. 或者直接使用xmanager enterprise,非 常方便. 如果在U ...

  10. 【sublime】解决汉字输入的办法——InputHelper;在sublime中输入汉字==》InputHelper方法

    手工安装办法: cd ~/.config/sublime-text-2/Packages  #如果是sublime3就改一下 git clone https://github.com/xgenvn/I ...