WPF学习之路(六)Command
在WPF中,命令绑定机制是相比于事件更高级的概念,把应用程序的功能划分为多个任务,任务由多种途径触发。
应用Command Binding使代码更符合MVVM模式(Model-View-ViewModel),类似于MVC模式(Model-View-Control)。这两种模式在以后的BLOG中会有详细的介绍。目的都是为了更好的分离前后台逻辑。
一个简单的Button
<Button Content="Button" Click="Button_Click" />
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello WPF");
}
前台显示需要通过Button_Click方法跟后台关联上,如果想更好的分离这两部分,将Click事件替换成Command
自定义Command
using System;
using System.Windows;
using System.Windows.Input; public class MyCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
} public event EventHandler CanExecuteChanged; public void Execute(object parameter)
{
MessageBox.Show("Hello WPF");
}
}
<Button Content="Button" x:Name="btn1" />
public MainWindow()
{
InitializeComponent();
btn1.Command = new MyCommand();
}
现在逻辑已经被分离到MyCommand中了
使用预定义Command
ApplicationCommands提供了很多预定义Command

<Button Content="Button" x:Name="btn2" Command="ApplicationCommands.Close"/>
但是这些命令并没有实现 ("▔□▔)
使用Command Binding添加逻辑
public MainWindow()
{
InitializeComponent();var OpenCmdBinding = new CommandBinding(
ApplicationCommands.Close,
OpenCmdExecuted,
OpenCmdCanExecute); this.CommandBindings.Add(OpenCmdBinding);
} void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
{
this.Close();
} void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
有些控件没有Command属性
<Button Grid.Row="" Height="" Width="" HorizontalAlignment="Left" Content="Decrease" Command="Slider.DecreaseLarge" CommandTarget="{Binding ElementName=slider}"/>
<Slider Grid.Row="" x:Name="slider" Width=""></Slider>
<Button Grid.Row="" Height="" Width="" HorizontalAlignment="Right" Content="Increase" Command="Slider.IncreaseLarge" CommandTarget="{Binding ElementName=slider}"/>
CommandParameter可以给命令传递一个值
CommandTarget:触发的命令目标
更多的Command介绍
http://www.cnblogs.com/Curry/archive/2009/07/27/1531798.html
http://www.cnblogs.com/gaixiaojie/archive/2010/09/01/1815015.html
To be continue...
WPF学习之路(六)Command的更多相关文章
- WPF学习之路初识
WPF学习之路初识 WPF 介绍 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 Windows Presentation Found ...
- 【WPF学习】第六十四章 构建基本的用户控件
创建一个简单用户控件是开始自定义控件的好方法.本章主要介绍创建一个基本的颜色拾取器.接下来分析如何将这个控件分解成功能更强大的基于模板的控件. 创建基本的颜色拾取器很容易.然而,创建自定义颜色拾取器仍 ...
- WPF学习之路一
前段时间一直在学习MVC,工作需要,现在需要180度急转弯,搞WPF,MVVM,只能找资料学习了. WPF中有一个消息机制,就是当前台控件绑定的值改变时,会自动通知到指定的事件来改变VM的值,反之亦然 ...
- WPF学习之路(九)导航链接
Hyperlink WPF中超链接类型是Hyperlink,除了能在页面之间导航,还能再同一个页面下进行段落导航 实例: <Grid> <FlowDocumentReader> ...
- WPF学习之路(一) 初识WPF
参考<葵花宝典-WPF自学手册> VS2012 先创建第一个WPF小程序 1.创建WPF程序 2.查看Solution,WPF中xaml文件和cs文件经常成对出现 两个主要的类:APP(W ...
- 【WPF学习】第六十章 创建控件模板
经过数十天的忙碌,今天终于有时间写博客. 前面一章通过介绍有关模板工作方式相关的内容,同时介绍了FrameWorkElement下所有控件的模板.接下来将介绍如何构建一个简单的自定义按钮,并在该过程中 ...
- WPF学习之路(十四)样式和模板
样式 实例: <Window.Resources> <Style x:Key="BtnStyle"> <Setter Property=" ...
- WPF学习之路(十三)URL
URL一般由三个部分组成,协议.资源所在主机地址.资源路径 WPF中URL同样有三部分组成:pack.authority(application:| siteoforigin:).路径 资源文件 本地 ...
- WPF学习之路(十二)控件(Items控件)
ListBox 提供了一个选项列表,可以固定或者动态绑定 <StackPanel> <GroupBox Margin="> <GroupBox.Header& ...
随机推荐
- 前端代码标准最佳实践:CSS
前端工程师对写标准的前端代码的重视程度很高.这些最佳标准实践并不是那个权威组织发布的,而是由大量的前端工程师们在实践过程中的经验总结,目的在于提高代码的可读性,可维护性和性能.那么接着上一篇,我们再来 ...
- [水煮 ASP.NET Web API2 方法论](3-6)万能路由
问题 定义什么样的路由,可以不会受请求参数类型和数量的限制,而被全部捕获? 解决方案 在路由模板中,给参数添加一个"*"前缀,例如 {*param},只要请求的 URL 能够和路由 ...
- C++移位运算符
关于逻辑移位.算术移位可参见迅雷深大笔试题部分.的一道题. 以前看到C++标准上说,移位运算符(<<.>>)出界时的行为并不确定: The behavior is undefi ...
- Javascript权威指南
一.数字写法 3.14 2345.789 .333333333333333333 6.02e23 // 6.02 × 10 23 1.4738223E-32 // 1.4738223 × 10 −32 ...
- jquery内容选择器(匹配包含指定选择器的元素)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Support for multiple result sets
https://blueprints.launchpad.net/myconnpy/+spec/sp-multi-resultsets Calling a stored procedure can p ...
- 【整理】 JavaScript模块化规范AMD 和 CMD 的区别有哪些?
根据玉伯等人在知乎上的回答整理.整理中... AMD 规范在这里:https://github.com/amdjs/amdjs-api/wiki/AMD CMD 规范在这里:https://githu ...
- django pdb
import pdb; pdb.set_trace() http://charlee.li/debug-djangp-with-pdb.html https://github.com/tomchr ...
- Mac上安装go环境
Mac 安装 GO语言开发环境 官网:https://golang.org/ go语言的安装:http://docscn.studygolang.com/doc/install 下载:go1.7rc3 ...
- Select-or-Die:灵活的 jQuery 下拉列表插件
Select-or-Die 是一个 jQuery 插件,用来自定义下拉列表(Select)元素.原生的下拉选择元素在各个浏览器的默认样式差异很多,而且自定义样式很困难,因此 Web 开发人员喜欢使用插 ...