基础类,继承与ICommand接口

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input; namespace WpfExample
{
public class RelayCommand : ICommand
{
#region Fields /// <summary>
/// Encapsulated the execute action
/// </summary>
private Action<object> execute; /// <summary>
/// Encapsulated the representation for the validation of the execute method
/// </summary>
private Predicate<object> canExecute; #endregion // Fields #region Constructors /// <summary>
/// Initializes a new instance of the RelayCommand class
/// Creates a new command that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action<object> execute)
: this(execute, DefaultCanExecute)
{
} /// <summary>
/// Initializes a new instance of the RelayCommand class
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
} if (canExecute == null)
{
throw new ArgumentNullException("canExecute");
} this.execute = execute;
this.canExecute = canExecute;
} #endregion // Constructors #region ICommand Members /// <summary>
/// An event to raise when the CanExecute value is changed
/// </summary>
/// <remarks>
/// Any subscription to this event will automatically subscribe to both
/// the local OnCanExecuteChanged method AND
/// the CommandManager RequerySuggested event
/// </remarks>
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
this.CanExecuteChangedInternal += value;
} remove
{
CommandManager.RequerySuggested -= value;
this.CanExecuteChangedInternal -= value;
}
} /// <summary>
/// An event to allow the CanExecuteChanged event to be raised manually
/// </summary>
private event EventHandler CanExecuteChangedInternal; /// <summary>
/// Defines if command can be executed
/// </summary>
/// <param name="parameter">the parameter that represents the validation method</param>
/// <returns>true if the command can be executed</returns>
public bool CanExecute(object parameter)
{
return this.canExecute != null && this.canExecute(parameter);
} /// <summary>
/// Execute the encapsulated command
/// </summary>
/// <param name="parameter">the parameter that represents the execution method</param>
public void Execute(object parameter)
{
this.execute(parameter);
} #endregion // ICommand Members /// <summary>
/// Raises the can execute changed.
/// </summary>
public void OnCanExecuteChanged()
{
EventHandler handler = this.CanExecuteChangedInternal;
if (handler != null)
{
//DispatcherHelper.BeginInvokeOnUIThread(() => handler.Invoke(this, EventArgs.Empty));
handler.Invoke(this, EventArgs.Empty);
}
} /// <summary>
/// Destroys this instance.
/// </summary>
public void Destroy()
{
this.canExecute = _ => false;
this.execute = _ => { return; };
} /// <summary>
/// Defines if command can be executed (default behaviour)
/// </summary>
/// <param name="parameter">The parameter.</param>
/// <returns>Always true</returns>
private static bool DefaultCanExecute(object parameter)
{
return true;
}
}
}

在VM中绑定对应命令的方法

   public ICommand ToggleExecuteCommand { get;set; }//前台绑定的命令
public ICommand HiButtonCommand { get; set; }//前台绑定的命令 public MainWindowViewModel()
{
HiButtonCommand = new RelayCommand(ShowMessage,CanExecute2);//初始化命令调用的方法
ToggleExecuteCommand = new RelayCommand(ChangeCanExecute);//初始化命令调用的方法
} private bool CanExecute2(object obj)//调用的方法体函数
{
return true;
} public void ShowMessage(object obj)//调用的方法体函数
{
MessageBox.Show(obj.ToString());
} public void ChangeCanExecute(object obj)//调用的方法体函数
{
//
}

WPF ICommand 用法的更多相关文章

  1. 基本MVVM 和 ICommand用法举例(转)

    引言 在本贴中,我们将学习WPF Commands. Commands 可以很好地与 MVVM 模式 (Model- View-ViewModel)结合在一起.我们也将看到,视图(view)实际上是怎 ...

  2. WPF DataTriger 用法示例代码

    用法1: <DataGridTemplateColumn Header="{lex:LocText ExamineRoom}"> <DataGridTemplat ...

  3. 【转】【WPF】WPF绑定用法

    一.简介 为了后面行文顺利,在进入正文之前,我们首先对本文所涉及到的绑定知识进行简单地介绍.该节包含绑定的基本组成以及构建方式. WPF中的绑定完成了绑定源和绑定目标的联动.一个绑定常常由四部分组成: ...

  4. WPF DataGrid 用法

    XAML==> <Window x:Class="QueueSystem.MainWindow" xmlns="http://schemas.microsof ...

  5. WPF StoryBoard用法

    时间:2011-06-15 21:26来源:百度空间 作者:shichen4 点击: 次 StoryBoard使用,Xaml转cs代码 Canvas.Triggers EventTriggerRout ...

  6. wpf icommand 命令接口

  7. WPF中StringFormat 格式化 的用法

    原文 WPF中StringFormat 格式化 的用法 网格用法 <my:DataGridTextColumn x:Name="PerformedDate" Header=& ...

  8. WPF中StringFormat的用法

    原文:WPF中StringFormat的用法 WPF中StringFormat的用法可以参照C#中string.Format的用法 1. C#中用法: 格式化货币(跟系统的环境有关,中文系统默认格式化 ...

  9. WPF 原生绑定和命令功能使用指南

    WPF 原生绑定和命令功能使用指南 魏刘宏 2020 年 2 月 21 日 如今,当谈到 WPF 时,我们言必称 MVVM.框架(如 Prism)等,似乎已经忘了不用这些的话该怎么使用 WPF 了.当 ...

随机推荐

  1. Windows下模拟Linux开发

    1.背景 Linux环境下开发是大势所趋,也是开发者必须掌握的技能.然windows系统已深入人心,实在不想放弃windows下的成熟应用,因此可以在Windows上模拟一个Linux系统.这样就满足 ...

  2. J2EE基础总结(1)——J2EE入门

    J2EE诞生的背景 在传统的开发模式(单层应用结构)下.应用普遍存在下面致命缺点: - 数据.页面和业务逻辑在一个逻辑层次中.功能紧密耦合. - 代码重用性极低,可维护性差. - 应用耦合度高,全然没 ...

  3. [Redux] Extracting Presentational Components -- AddTodo

    The code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { ...

  4. grub2手动引导ubuntu

    測试机OS为ubuntu 14.04.1 LTS x86_64 磁盘分区情况为: Filesystem     1K-blocks    Used Available Use% Mounted on ...

  5. CSS样式之背景、文本

    一.背景     1.背景颜色用background-color属性,例如:body{background-color:red}     2.用图像做背景用background-image属性,例如b ...

  6. 严重: Exception starting filter struts2 --Unable to load configuration

    严重: Exception starting filter struts2 Unable to load configuration. - [unknown location] at com.open ...

  7. iOS_SN_push/pop转场动画封装和一般动画封装

    封装类中的方法: #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface AnimationE ...

  8. (三)原生JS实现 - 插件 - 弹出层

    创建遮罩层 _createCover: function() { var newMask = document.createElement("div"); newMask.id = ...

  9. MDX示例:求解中位数、四分位数(median、quartile)

    一个人力资源咨询集团通过网络爬虫采集手段将多个知名招聘网站上发布的求职和招聘等信息准实时采集到自己的库里,形成一个数据量浩大的招聘信息库,跟踪全国招聘和求职的行业.工种.职位.待遇等信息,并通过商业智 ...

  10. J2EE 基础知识积累

    1. 面向对象的思维: 1. 有哪些类 那些对象      2. 这些类中,每种类应该具有某种属性和方法      3. 考虑类与类之间应该具有什么样的关系 3. 1. 成员变量可以使用java语言中 ...