WPF中的命令简介
使用Prism委托命令Demo: WPF委托命令DelegateCommand的传参方式
在WPF中使用命令的步骤很简单
1.创建命令
2.绑定命令
3.设置命令源
4.设置命令目标
WPF中命令的核心是System.Windows.Input.ICommand接口,所有命令对象都实现了此接口。当创建自己的命令时,不能直接实现ICommand接口,而是要使用System.Windows.Input.RouteCommand类,该类已经实现了ICommand接口,所有WPF命令都是RouteCommand类的实例。在程序中处理的大部分命令不是RoutedCommand对象,而是RoutedUICommand类的实例,它继承自RouteCommand类。
WPF提供了一个命令库,命令库中提供了多个常用的命令,命令库通过5个专门的静态类的静态属性来提供。
5个静态类分别为:
ApplicationCommands 该类提供通用命令,包括Copy、Cut、Paste等等。
NavigationCommands 该类提供了导航的命令。
EditingCommands 该类提供了很多主要的文档编辑命令 如MoveToLineEnd、MoveLeftByWord等等。
CommponentCommands 该类提供了用户界面元素使用的命令。
MediaCommands 该类提供了一组用于处理多媒体的命令。
MSDN帮助文档搜索ApplicaiontCommands、NavigationCommands等可以看到命令详细用法
通过上面5个静态类的静态属性可以获得常用的命令对象。
下面的XAML示例演示了将命令源关联到按钮,代码如下。
<Window x:Class="WPF命令详解.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" WindowStartupLocation="CenterScreen"
Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Command="ApplicationCommands.Open"></Button>
<Button Grid.Row="1" Command="ApplicationCommands.New"></Button>
<Button Grid.Row="2" Command="ApplicationCommands.Save"></Button>
<Button Grid.Row="3" Command="ApplicationCommands.Copy"></Button>
</Grid>
</Window>
从上面的代码中可以看到,通过Command关联命令对象,当应用程序执行时,会发现按钮都是不可用的,变成了不可用状态与IsEnable属性设置为False一样。这是因为按钮还没有关联绑定,下面看一下关键绑定后的代码如下。
XAML代码如下。
<Window x:Class="WPF命令详解.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" WindowStartupLocation="CenterScreen"
Loaded="Window_Loaded">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy"
Executed="CommandBinding_Executed">
</CommandBinding>
</Window.CommandBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Command="ApplicationCommands.Open"></Button>
<Button Grid.Row="1" Command="ApplicationCommands.New"></Button>
<Button Grid.Row="2" Command="ApplicationCommands.Save"></Button>
<Button Grid.Row="3" Command="ApplicationCommands.Copy"></Button>
</Grid>
</Window>
CS代码如下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace WPF命令详解
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
CommandBinding binding = new CommandBinding(ApplicationCommands.New);
binding.Executed += new ExecutedRoutedEventHandler(binding_Executed);
CommandBinding cmd_Open = new CommandBinding(ApplicationCommands.Open);
cmd_Open.Executed += new ExecutedRoutedEventHandler(cmd_Open_Executed);
CommandBinding cmd_Save = new CommandBinding(ApplicationCommands.Save);
cmd_Save.Executed += new ExecutedRoutedEventHandler(cmd_Save_Executed); this.CommandBindings.Add(binding);
this.CommandBindings.Add(cmd_Open);
this.CommandBindings.Add(cmd_Save);
} void cmd_Save_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("保存");
} void cmd_Open_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("打开");
} void binding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("新建");
} private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("复制");
}
}
}
从上面的代码中可以看到,在XAML代码中可以实现命令绑定。
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy"
Executed="CommandBinding_Executed">
</CommandBinding>
</Window.CommandBindings>
也可以在CS代码中实现命令绑定。
CommandBinding binding = new CommandBinding(ApplicationCommands.New);
binding.Executed += new ExecutedRoutedEventHandler(binding_Executed);
this.CommandBindings.Add(binding);
还有就是要写Executed事件中的代码。
void binding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("新建");
}
上面的内容是通过实现了ICommandSource接口的Button控件来触发执行的命令,下面演示了直接调用命令的方式,代码如下。
ApplicationCommands.Open.Execute(null, this);
CommandBindings[].Command.Execute(null);
第一种方法使用了命令对象的Execute方法调用命令,此方法接收两个参数,第一个参数是传递的参数值,第二个参数是命令绑定的所在位置,示例中使用了当前窗体。
第二种方法在关联的CommandBinding对象中调用Execute方法,对于这种情况不需要提供命令绑定的所在位置,因为会自动将提供正在使用的CommandBindings集合的元素设置为绑定位置。
WPF中的命令简介的更多相关文章
- 16、WPF中的命令
一.前言 事件的作用是发布.传播一些信息,消息送达接收者,事件的使命就算完成了,至于如何响应事件送来的消息事件并不做规定,每个接收者可以使用自己的行为来响应事件,也就是说事件不具有约束力.命令能够在代 ...
- WPF中的命令与命令绑定导航
1.WPF中的命令与命令绑定(一) (引入命令) 2.WPF中的命令与命令绑定(二)(详细介绍命令和命令绑定)
- WPF中的命令与命令绑定(二)
原文:WPF中的命令与命令绑定(二) WPF中的命令与命令绑定(二) 周银辉在WPF中,命令(Commandi ...
- WPF中的命令与命令绑定(一)
原文:WPF中的命令与命令绑定(一) WPF中的命令与命令绑定(一) 周银辉说到用户输入,可能我们更多地会联想到 ...
- Windows Presentation Foundation (WPF)中的命令(Commands)简述
原文:Windows Presentation Foundation (WPF)中的命令(Commands)简述 ------------------------------------------- ...
- WPF中的图像处理简介
原文:WPF中的图像处理简介 和Winform中的GDI+相比,WPF提供了一组新的API用于显示和编辑图像.新API特点如下: 适用于新的或专用图像格式的扩展性模型. 对包括位图 (BMP).联合图 ...
- WPF中的资源简介、DynamicResource与StaticResource的区别
原文:WPF中的资源简介.DynamicResource与StaticResource的区别 什么叫WPF的资源(Resource)?资源是保存在可执行文件中的一种不可执行数据.在WPF的资源中,几乎 ...
- WPF中的命令(Command)
这节来讲一下WPF中的命令(Command)的使用. [认识Command] 我们之前说过,WPF本身就为我们提供了一个基础的MVVM框架,本节要讲的命令就是其中一环,通过在ViewModel中声明命 ...
- WPF中的资源简介、DynamicResource与StaticResource的区别(转)
什么叫WPF的资源(Resource)?资源是保存在可执行文件中的一种不可执行数据.在WPF的资源中,几乎可以包含图像.字符串等所有的任意CLR对象,只要对象有一个默认的构造函数和独立的属性. 也就是 ...
随机推荐
- SQLAlchemy(1) -- Python的SQLAlchemy和ORM
Python的SQLAlchemy和ORM(object-relational mapping:对象关系映射) web编程中有一项常规任务就是创建一个有效的后台数据库.以前,程序员是通过写sql语句, ...
- Android事件处理的2种方式:监听器与回调
android组件的事件处理有2种方式: 1.基于监听器的事件处理方式:先定义组件,然后为组件设定监听器. 详见http://blog.csdn.net/jediael_lu/article/deta ...
- 【原创】纯干货,Spring-data-jpa详解,全方位介绍。(转)
本篇进行Spring-data-jpa的介绍,几乎涵盖该框架的所有方面,在日常的开发当中,基本上能满足所有需求.这里不讲解JPA和Spring-data-jpa单独使用,所有的内容都是在和Spring ...
- SpringBoot+mybatis实现多数据源支持
什么是多数据源支持? 简单的说,就是一个项目里,同时可以访问多个不同的数据库. 实现原理 单个数据源在配置时会绑定一套mybatis配置,多个数据源时,不同的数据源绑定不同的mybatis配置就可以了 ...
- Xtrabackup备份、还原、恢复Mysql操作大全
环境:CentOS 6.7 + Mysql 5.7.19 + Xtraback 2.4.8 innobackupex常用参数: --user=USER 指定备份用户,不指定的话为当前系统用户 --p ...
- OpenCV Machine Learning 之 K近期邻分类器的应用 K-Nearest Neighbors
OpenCV Machine Learning 之 K近期邻分类器的应用 以下的程序实现了对高斯分布的点集合进行分类的K近期令分类器 #include "ml.h" #includ ...
- Eclipse中如何安装和使用GrepCode插件 (转)
GrepCode(GC)Eclipse插件允许Eclipse用户在Eclipse IDE中搜索由GrepCode提供的工厂类.本教程介绍如何安装和使用插件.使用Eclipse3.5(Galileo)的 ...
- nginx 配置http重定向到https
在80端口的那个server下,添加如下: server_name www.youwebsite.com youwebsite.com; rewrite ^(.*)$ https://$host$1 ...
- Quartz.Net定时任务EF+MVC版的web服务
之前项目采用JAVA 的 Quartz 进行定时服调度务处理程序,目前在.NET下面使用依然可以完成相同的工作任务,其实什么语言不重要,关键是我们要学会利用语言实现价值.它是一个简单的执行任务计划的组 ...
- jQuery中 index() 方法的使用
假设一个集合中有10个元素,源生js在添加事件的时候,会使用for循环,里面的i的值,就是当前点击元素是集合中的第i个元素.在jquery中,获得i的值的方法如下: <ul id="a ...