WPF ICommand 用法
基础类,继承与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 用法的更多相关文章
- 基本MVVM 和 ICommand用法举例(转)
引言 在本贴中,我们将学习WPF Commands. Commands 可以很好地与 MVVM 模式 (Model- View-ViewModel)结合在一起.我们也将看到,视图(view)实际上是怎 ...
- WPF DataTriger 用法示例代码
用法1: <DataGridTemplateColumn Header="{lex:LocText ExamineRoom}"> <DataGridTemplat ...
- 【转】【WPF】WPF绑定用法
一.简介 为了后面行文顺利,在进入正文之前,我们首先对本文所涉及到的绑定知识进行简单地介绍.该节包含绑定的基本组成以及构建方式. WPF中的绑定完成了绑定源和绑定目标的联动.一个绑定常常由四部分组成: ...
- WPF DataGrid 用法
XAML==> <Window x:Class="QueueSystem.MainWindow" xmlns="http://schemas.microsof ...
- WPF StoryBoard用法
时间:2011-06-15 21:26来源:百度空间 作者:shichen4 点击: 次 StoryBoard使用,Xaml转cs代码 Canvas.Triggers EventTriggerRout ...
- wpf icommand 命令接口
- WPF中StringFormat 格式化 的用法
原文 WPF中StringFormat 格式化 的用法 网格用法 <my:DataGridTextColumn x:Name="PerformedDate" Header=& ...
- WPF中StringFormat的用法
原文:WPF中StringFormat的用法 WPF中StringFormat的用法可以参照C#中string.Format的用法 1. C#中用法: 格式化货币(跟系统的环境有关,中文系统默认格式化 ...
- WPF 原生绑定和命令功能使用指南
WPF 原生绑定和命令功能使用指南 魏刘宏 2020 年 2 月 21 日 如今,当谈到 WPF 时,我们言必称 MVVM.框架(如 Prism)等,似乎已经忘了不用这些的话该怎么使用 WPF 了.当 ...
随机推荐
- 必看谷歌HTML/CSS规范
背景 这篇文章定义了 HTML 和 CSS 的格式和代码规范,旨在提高代码质量和协作效率. 通用样式规范 协议 省略图片.样式.脚本以及其他媒体文件 URL 的协议部分( http:,https: ) ...
- python数据类型—列表(增改删查,统计,取值,排序)
列表是最常用的数据类型之一,通过列表可以对数据实现方便的存储,修改等操作. 先声明一个空列表: >>> names = [] >>> names [] 可以存多个值 ...
- hdu 4640 Island and study-sister
bfs+状态压缩求出所有的状态,然后由于第一个节点需要特殊处理,可以右移一位剔除掉,也可以特判.然后采用集合的操作, #pragma comment(linker,"/STACK:10240 ...
- 设计模式之Application Programs and Toolkits
Application Programs 应用程序 If you're building an application programsuch as a document editor or spre ...
- Smack+Openfire 接收和发送文件
转载请注明出处:http://blog.csdn.net/steelychen/article/details/37958839 发送文件须要提供准确的接收放username称(例:user2@192 ...
- crtmpserver通常使用基本类演示
以前我们做了分析过程,这一次,我们都参与了类做梳子,两个可以一起关注一下一起合并,整个方案的实施是有帮助. BaseClientApplication APP基类,一切APP都基于这个类 Stream ...
- IT痴汉的工作现状25-技术之养成
要想成为技术大牛,除了天赋以外,更与后天的刻苦努力分不开.伟仔我天生愚顿.工作多年后仍与大牛相差甚远,更加觉得技术的养成是一个异常困难的过程. 是我不用功吗?我不这样觉得.伟仔尽管是个懒人,但对于技术 ...
- EL 表达式中自己定义函数
第一步: 在WEB-INF/tld/ 文件夹下创建一个func.tld文件例如以下: <taglib xmlns="http://java.sun.com/xml/ns/j2ee&qu ...
- 使用C++11实现无锁stack(lock-free stack)
前几篇文章,我们讨论了如何使用mutex保护数据及使用使用condition variable在多线程中进行同步.然而,使用mutex将会导致一下问题: 等待互斥锁会消耗宝贵的时间 — 有时候是很多时 ...
- public,private,protected的区别
一,public,private,protected的区别public:权限是最大的,可以内部调用,实例调用等.protected: 受保护类型,用于本类和继承类调用.private: 私有类型,只有 ...