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. 异步调用webservice

    一.异步调用 asynchronous call(异步调用):一个可以无需等待被调用函数的返回值就让操作继续进行的方法 举例: 异步调用就是你 喊 你朋友吃饭 ,你朋友说知道了 ,待会忙完去找你 ,你 ...

  2. SQL Sever 身份验证 sa用户设置

    1.用windows身份验证登陆数据库找到sa用户 2.鼠标右键sa->属性->常规,设置密码. 3.选择状态->登陆选择已启用 4.选中当前数据库 鼠标右键->属性 5.选择 ...

  3. java的I/O操作:文件的路径

    package solutions; import java.io.*; /** * Created by Administrator on 2016/3/14. */ public class Re ...

  4. 一群猴子排成一圈,按1,2,...,n依次编号。然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去...,如此不停的进行下去,直到最后只剩下一只猴子为止,那只猴子就叫做大王。要求编程模拟此过程,输入m、n, 输出最后那个大王的编号

    <?php/** * [猴子选大王] * @param  [type] $m [猴子数] * @param  [type] $n [出局次数] * @return [type]    [desc ...

  5. paper 72 :高动态范围(HDR)图像 HDR (High Dynamic Range)

    In standard rendering, the red, green and blue values for a pixel are each represented by a fraction ...

  6. 夺命雷公狗---node.js---12之fs模块文件的操作

    node比客户端浏览器的js强的地方之一就是他的文件操作模块,可以直接对系统的文件进行操作 再打开来看下是否发生了变化,由此可见node的强大的地方了.. 实际代码如下所示: /** * Create ...

  7. [php] 使用IDE的正则搜索代码

    ([^a-zA-Z_=$0-9/\[\>])('|"|,)?(\s*)store_banner(\s*)('|"|,)?([^a-zA-Z_=$0-9\/\(\]:]) 用在 ...

  8. 【海岛帝国系列赛】No.7 海岛帝国:神圣之日

    50237242海岛帝国:神圣之日 [试题描述] 战争持续九个月了.“购物券”WHT的军队还在跟恐怖分子僵持着.WHT和LJX已经向“公务员”告急,情况不宜乐观.YSF为守护帝国决定打开“够累 的”星 ...

  9. IO细述

    Java IO1:IO和File IO 大多数的应用程序都要与外部设备进行数据交换,最常见的外部设备包含磁盘和网络.IO就是指应用程序对这些设备的数据输入与输出,Java语言定义了许多类专门负责各种方 ...

  10. Angularjs之表单实例(三)

    正确引用js css文件后可运行 <!DOCTYPE html> <html ng-app='myApp'> <head> <title>Bootstr ...