整理:WPF中CommandBindings的用法
目的:了解一下CommandBindings、InputBindings、ICommandSource中在WPF中扮演什么样的角色
CommandBindings:
将应用程序要执行的功能封装到中间层组件或独立的类中:可以将所有命令统一封装,统一管理,统一调用
定义过程:
-
//将所有命令封装在一个类里面
-
public class MyCommands
-
{
-
public static RoutedUICommand MyCommand = new RoutedUICommand();
-
-
public static void DoCommand()
-
{
-
MyCommand.Execute("erer",null);
-
}
-
-
}
说明:定义一个类用来存放所有Binding命令,定义一个MyCommand命令供页面使用
Xaml中注册定义的命令:
-
<!--第一步,将整个窗口要用到的命令注册-->
-
<UserControl.CommandBindings>
-
<CommandBinding Command="local:MyCommands.MyCommand"
-
Executed="CommandBinding_Executed"
-
CanExecute="CommandBinding_CanExecute"/>
-
-
<!--如果CanExecute返回false则注册该命令的所有控件是不可用的-->
-
</UserControl.CommandBindings>
Xaml中将命令赋值到需要调用该命令的ICommandSource对象
-
<Grid>
-
<StackPanel>
-
<Menu>
-
<!--实现了ICommandSource接口的控件都可以直接赋值到Command命令上-->
-
<MenuItem Command="local:MyCommands.MyCommand"
-
Header="menu"
-
CommandParameter="1111" />
-
</Menu>
-
<Button Command="local:MyCommands.MyCommand" Height="50" Content="执行命令"
-
CommandParameter="button"
-
Grid.Row="1"/>
-
-
<CheckBox Content="控制MyCommand是否可以执行" x:Name="cb" IsChecked="True"/>
-
-
-
<Button Content="外部触发命令" Click="Button_Click"/>
-
</StackPanel>
-
</Grid>
ICommandSource对象 Command="local:MyCommands.MyCommand" 赋值会触发注册的MyCommand命令
注意:
<CommandBinding Command="local:MyCommands.MyCommand"
Executed="CommandBinding_Executed"
CanExecute="CommandBinding_CanExecute"/>
Command只能传入静态的命令,并通过Executed、CanExecute注册要实现的事件,不可以{Binding }到ViewModel
KeyBinding:
可以直接注册快捷键、鼠标
-
<UserControl.InputBindings>
-
<KeyBinding Command="local:MyCommands.MyCommand" Key="T" Modifiers="Alt"/>
-
</UserControl.InputBindings>
说明:将快捷键Alt+T注册到命令local:MyCommands.MyCommand上,也可以对Command 执行{Binding}到ViewModel,本质上也是一个ICommandSource对象
也可以用在Xaml中这样写
-
<UserControl.Resources>
-
<RoutedUICommand x:Key="Cut" Text="剪切" />
-
<RoutedUICommand x:Key="Copy" Text="复制" />
-
<RoutedUICommand x:Key="Paste" Text="粘贴" />
-
<RoutedUICommand x:Key="Select" Text="全选" />
-
</UserControl.Resources>
-
<UserControl.InputBindings>
-
<KeyBinding Gesture="Ctrl+X" Command="{StaticResource Cut}" />
-
<KeyBinding Gesture="Ctrl+C" Command="{StaticResource Copy}" />
-
<KeyBinding Gesture="Ctrl+V" Command="{StaticResource Paste}" />
-
</UserControl.InputBindings>
-
<UserControl.CommandBindings>
-
<CommandBinding Command="{StaticResource Cut}" Executed="CommandBinding_Cut"></CommandBinding>
-
<CommandBinding Command="{StaticResource Copy}" Executed="CommandBinding_Copy"></CommandBinding>
-
<CommandBinding Command="{StaticResource Paste}" Executed="CommandBinding_Paste"></CommandBinding>
-
</UserControl.CommandBindings>
整理:WPF中CommandBindings的用法的更多相关文章
- 整理:WPF中XmlDataProvider的用法总结
原文:整理:WPF中XmlDataProvider的用法总结 一.目的:了解XmlDataProvider中绑定数据的方法 二.绑定方式主要有三种: 1.Xaml资源中内置: <!--XPath ...
- WPF中StringFormat的用法
原文:WPF中StringFormat的用法 WPF中StringFormat的用法可以参照C#中string.Format的用法 1. C#中用法: 格式化货币(跟系统的环境有关,中文系统默认格式化 ...
- WPF中StringFormat的用法--显示特定位数的数字
原文:WPF中StringFormat的用法--显示特定位数的数字 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/huangli321456/art ...
- WPF中log4net的用法
WPF中如何使用log4nethttp://www.cnblogs.com/C-Sharp2/archive/2013/04/12/WPF-LOG4NET.html Apache log4net Ma ...
- wpf中INotifyPropertyChanged的用法
using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using Sy ...
- 整理:WPF中Binding的几种写法
原文:整理:WPF中Binding的几种写法 目的:整理WPF中Bind的写法 <!--绑定到DataContext--> <Button Content="{Bindin ...
- 在 WPF 中的线程
线程处理使程序能够执行并发处理,以便它可以做多个操作一次.节省开发人员从线程处理困难的方式,设计了 WPF (窗口演示文稿基金会).这篇文章可以帮助理解线程在 WPF 中的正确用法. WPF 内部线程 ...
- WPF中的常用布局 栈的实现 一个关于素数的神奇性质 C# defualt关键字默认值用法 接口通俗理解 C# Json序列化和反序列化 ASP.NET CORE系列【五】webapi整理以及RESTful风格化
WPF中的常用布局 一 写在开头1.1 写在开头微软是一家伟大的公司.评价一门技术的好坏得看具体的需求,没有哪门技术是面面俱到地好,应该抛弃对微软和微软的技术的偏见. 1.2 本文内容本文主要内容 ...
- WPF中StringFormat 格式化 的用法
原文 WPF中StringFormat 格式化 的用法 网格用法 <my:DataGridTextColumn x:Name="PerformedDate" Header=& ...
随机推荐
- MySQL 重要参数 innodb_flush_log_at_trx_commit 和 sync_binlog
innodb_flush_log_at_trx_commit 主要控制了innodb将log buffer中的数据写入日志文件并flush磁盘的时间点,取值分别为0.1.2三个.该参数控制重做日志写入 ...
- Jmeter 使用自定义变量
有些情况下比如发起测试时URL的主机名和端口需要在采样器中出现多次,这样就有个问题,当测试的主机更改时, 我们需要修改主机名称,这时就需要修改多个地方,如果多的情况会有遗漏.如果我们在配置脚本的时候, ...
- Java字符串——String深入
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10840495.html 一:字符串的不可变性 1.可变 与 不可变 辨析 Java中的对象按照创建后,对象的 ...
- 爬虫篇-python爬虫中多线程的使用
queue介绍 queue是python的标准库,俗称队列.可以直接import引用,在python2.x中,模块名为Queue.python3直接queue即可 在python中,多个线程之间的数据 ...
- 08-人脸识别-FaceNet-classify.py代码阅读(说明见注释)
"""An example of how to use your own dataset to train a classifier that recognizes pe ...
- tomcat相关知识点
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用. tomcat的文件结构: bin:用于存放启动和关闭tom ...
- javascript中 encodeURIComponent() 与 encodeURI() 的区别
前言:js 中仅有的几个全局函数中,有两个全局函数可以用来编码url 字符串. 一.encodeURIComponent() 将转义用于分隔 URI 各个部分的标点符号 ,也就是可以编码 " ...
- 关于很狗的军训qwq
我该用哪种方式来证明我存在过 说起这个很狗的军训呀,,, 军训嘛 就不得不先说最基本的,,, 那就是很狗的衣服,,, 一套军装,+一个帽子+一双特别特别特别硌脚的胶鞋!!! 然后... 我胖了..q ...
- Spring Boot 2.2.1 发布,一个有点坑的版本!
上一篇:Spring Boot 2.2.0 正式发布,支持 JDK 13! Spring Boot 2.2.0 没发布多久,Spring Boot 2.2.1 又发布了,这是一个很有意思,又有点 &q ...
- js设置元素指定时间隐藏
$().fadeOut(); js指定时间隐藏