原文:整理:WPF中CommandBindings的用法

目的:了解一下CommandBindings、InputBindings、ICommandSource中在WPF中扮演什么样的角色

CommandBindings:

将应用程序要执行的功能封装到中间层组件或独立的类中:可以将所有命令统一封装,统一管理,统一调用

定义过程:


  1. //将所有命令封装在一个类里面
  2. public class MyCommands
  3. {
  4. public static RoutedUICommand MyCommand = new RoutedUICommand();
  5. public static void DoCommand()
  6. {
  7. MyCommand.Execute("erer",null);
  8. }
  9. }

说明:定义一个类用来存放所有Binding命令,定义一个MyCommand命令供页面使用

Xaml中注册定义的命令:


  1. <!--第一步,将整个窗口要用到的命令注册-->
  2. <UserControl.CommandBindings>
  3. <CommandBinding Command="local:MyCommands.MyCommand"
  4. Executed="CommandBinding_Executed"
  5. CanExecute="CommandBinding_CanExecute"/>
  6. <!--如果CanExecute返回false则注册该命令的所有控件是不可用的-->
  7. </UserControl.CommandBindings>

Xaml中将命令赋值到需要调用该命令的ICommandSource对象


  1. <Grid>
  2. <StackPanel>
  3. <Menu>
  4. <!--实现了ICommandSource接口的控件都可以直接赋值到Command命令上-->
  5. <MenuItem Command="local:MyCommands.MyCommand"
  6. Header="menu"
  7. CommandParameter="1111" />
  8. </Menu>
  9. <Button Command="local:MyCommands.MyCommand" Height="50" Content="执行命令"
  10. CommandParameter="button"
  11. Grid.Row="1"/>
  12. <CheckBox Content="控制MyCommand是否可以执行" x:Name="cb" IsChecked="True"/>
  13. <Button Content="外部触发命令" Click="Button_Click"/>
  14. </StackPanel>
  15. </Grid>

ICommandSource对象 Command="local:MyCommands.MyCommand"  赋值会触发注册的MyCommand命令

注意:

<CommandBinding Command="local:MyCommands.MyCommand" 

                        Executed="CommandBinding_Executed" 

                        CanExecute="CommandBinding_CanExecute"/>

Command只能传入静态的命令,并通过Executed、CanExecute注册要实现的事件,不可以{Binding }到ViewModel

KeyBinding:

可以直接注册快捷键、鼠标


  1. <UserControl.InputBindings>
  2. <KeyBinding Command="local:MyCommands.MyCommand" Key="T" Modifiers="Alt"/>
  3. </UserControl.InputBindings>

说明:将快捷键Alt+T注册到命令local:MyCommands.MyCommand上,也可以对Command 执行{Binding}到ViewModel,本质上也是一个ICommandSource对象

也可以用在Xaml中这样写


  1. <UserControl.Resources>
  2. <RoutedUICommand x:Key="Cut" Text="剪切" />
  3. <RoutedUICommand x:Key="Copy" Text="复制" />
  4. <RoutedUICommand x:Key="Paste" Text="粘贴" />
  5. <RoutedUICommand x:Key="Select" Text="全选" />
  6. </UserControl.Resources>
  7. <UserControl.InputBindings>
  8. <KeyBinding Gesture="Ctrl+X" Command="{StaticResource Cut}" />
  9. <KeyBinding Gesture="Ctrl+C" Command="{StaticResource Copy}" />
  10. <KeyBinding Gesture="Ctrl+V" Command="{StaticResource Paste}" />
  11. </UserControl.InputBindings>
  12. <UserControl.CommandBindings>
  13. <CommandBinding Command="{StaticResource Cut}" Executed="CommandBinding_Cut"></CommandBinding>
  14. <CommandBinding Command="{StaticResource Copy}" Executed="CommandBinding_Copy"></CommandBinding>
  15. <CommandBinding Command="{StaticResource Paste}" Executed="CommandBinding_Paste"></CommandBinding>
  16. </UserControl.CommandBindings>

整理:WPF中CommandBindings的用法的更多相关文章

  1. 整理:WPF中XmlDataProvider的用法总结

    原文:整理:WPF中XmlDataProvider的用法总结 一.目的:了解XmlDataProvider中绑定数据的方法 二.绑定方式主要有三种: 1.Xaml资源中内置: <!--XPath ...

  2. WPF中StringFormat的用法

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

  3. WPF中StringFormat的用法--显示特定位数的数字

    原文:WPF中StringFormat的用法--显示特定位数的数字 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/huangli321456/art ...

  4. WPF中log4net的用法

    WPF中如何使用log4nethttp://www.cnblogs.com/C-Sharp2/archive/2013/04/12/WPF-LOG4NET.html Apache log4net Ma ...

  5. wpf中INotifyPropertyChanged的用法

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using Sy ...

  6. 整理:WPF中Binding的几种写法

    原文:整理:WPF中Binding的几种写法 目的:整理WPF中Bind的写法 <!--绑定到DataContext--> <Button Content="{Bindin ...

  7. 在 WPF 中的线程

    线程处理使程序能够执行并发处理,以便它可以做多个操作一次.节省开发人员从线程处理困难的方式,设计了 WPF (窗口演示文稿基金会).这篇文章可以帮助理解线程在 WPF 中的正确用法. WPF 内部线程 ...

  8. WPF中的常用布局 栈的实现 一个关于素数的神奇性质 C# defualt关键字默认值用法 接口通俗理解 C# Json序列化和反序列化 ASP.NET CORE系列【五】webapi整理以及RESTful风格化

    WPF中的常用布局   一 写在开头1.1 写在开头微软是一家伟大的公司.评价一门技术的好坏得看具体的需求,没有哪门技术是面面俱到地好,应该抛弃对微软和微软的技术的偏见. 1.2 本文内容本文主要内容 ...

  9. WPF中StringFormat 格式化 的用法

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

随机推荐

  1. mybatis中的高级查询

    Mybatis作为一个ORM框架,肯定是支持sql高级查询的. 下面的一个案例来为大家详细讲解mybatis中的高级查询. 案例说明: 此案例的业务关系是用户,订单,订单详情与商品之间的关系. 以订单 ...

  2. MySQL Error--InnoDB Table mysqlinnodb_index_stats has length mismatch in the column

    使用MySQL 5.7.24版本的安装文件替换MySQL 5.7.19版本的安装文件,数据库复制频繁中断,查看error日志发现下面错误: [Warning] InnoDB: Table mysql/ ...

  3. 编写合格的C代码(1):通过编译选项将特定警告视为错误

    目录 快速设定 向错误的执念开炮,向C编译器开炮 编译警告应当被忽略吗?warning不重要吗? 个人总结的应当视作error的warning 1. 函数没有声明就使用 2. 函数虽然有声明,但是声明 ...

  4. websocket 的基本用法

    项目当中使用到了websocket,以前的项目当中使用到了另外一个类似的socket.io,两者的区别和联系在另外一篇文章当中有提及,这里就简单的写下websocket的用法 下面的例子是阮一峰的We ...

  5. GitHub的SSH key配置以及常用的git命令介绍

    一. GitHub的SSH key配置 (以windows为例,Mac iOS系统类似) SSH Key 是一种方法来确定受信任的计算机,从而实现免密码登录.Git是分布式的代码管理工具,远程的代码管 ...

  6. 重新学习Spring注解——ICO

    02.组件注册-@Configuration&@Bean给容器中注册组件 03.组件注册-@ComponentScan-自动扫描组件&指定扫描规则 04.组件注册-自定义TypeFil ...

  7. nodemcu固件的烧录及lua开发

    一.板子介绍 NodeMCU 1.0/ESP 8266 12E 该模块是安信可公司生产的,并且提供全部开发资料. 对该模块的开发有两种方式: 一种是基于乐鑫官方推出的SDK开发包在 安信可ESP的一体 ...

  8. 配置Maven环境变量-Eclipse/Idea添加Maven

    1. 文件下载 官网下载地址:http://maven.apache.org/download.cgi 下方有我提供的下载链接. 由于下载缓慢,提供一份我的下载链接:https://www.lanzo ...

  9. Centos7安装MySQL(多图)

    文章目录 一.在线安装1.替换网易yum源2.清理缓存3.下载rpm文件4.安装MySQL数据库二.本地安装1.上传MySQL安装包2.安装依赖的程序包3.卸载mariadb程序包4.安装MySQL程 ...

  10. ElementUI_NodeJS环境搭建

    ElementUI简介 我们学习VUE,知道它的核心思想式组件和数据驱动,但是每一个组件都需要自己编写模板,样式,添加事件,数据等是非常麻烦的, 所以饿了吗推出了基于VUE2.0的组件库,它的名称叫做 ...