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 了.当 ...
随机推荐
- python数据类型-布尔值
布尔是计算机里最基本的判断单位,布尔只有两个值:真或假,即True False,也就是1或0. 在以后学习流程控制时会经常用到布尔值. 先来看简单的小例子: >>> 1+1 > ...
- jQuery的ajax jsonp跨域请求
了解:ajax.json.jsonp.“跨域”的关系 要弄清楚以上ajax.json.jsonp概念的关系,我觉得弄清楚ajax是“干什么的”,“怎么实现的”,“有什么问题”,“如果解决存在的问题”等 ...
- C++内存分配的五种方法
在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储区. 栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区.里面的变量通常是局部变量.函数参数 ...
- asp.net实现将网页内容输出到word并下载到本地
个人觉得要实现这个功能如果没有类库提供的几个关键函数,还是比较繁琐的.所以首先介绍几个将要在代码中使用的关键函数和参数,然后再说函数实现.注意问题等. 关键函数: 1.函数原型:Response.Ap ...
- 利用“参数赋值”防范SQL注入漏洞攻击
<<年轻,无权享受>————送给每一个看到此文的同僚们 在这无精打采的炎夏 我躺在阳台上房东的旧沙发 回想几个月来遇到的问题 我不禁内心开始慌张喘着粗气 还有大把时间去打拼 没有到只 ...
- poj2709 贪心基础
D - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bi ...
- linux查看压缩包的文件列表
网上看到了一篇文章: Using bzip2 with less 这篇文章介绍了一个脚本,脚本功能就是列出压缩包所压缩的文件,本文算是原文搬运,不过减少点东西以适用我日常系统运用. #!/bin/ba ...
- 滚动到指定元素的id处+当元素出现在浏览器显示区域就会自动加载
//滚动到指定元素的id处 如:$("#Exam82") function Jump() { var scroll_offset = $("#Exam82"). ...
- Asp.net mvc 3 file uploads using the fileapi
Asp.net mvc 3 file uploads using the fileapi I was recently given the task of adding upload progress ...
- Destoon标签使用技巧十则
Destoon标签 1.全局标签 网站名称:{$DT[sitename]}网站地址:{DT_PATH}网站LOGO: {if $MODULE[$moduleid][logo]}{DT_SKIN}ima ...