mvvm command的使用案例
主界面如下:

前台代码:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel x:Name="stackPanel" Margin="20">
<Button x:Name="button1" Content="命令测试" />
<TextBox x:Name="textBoxA" />
</StackPanel>
</Grid>
</Window>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private RoutedCommand clearCmd = new RoutedCommand("Clear", typeof(MainWindow));
public MainWindow()
{
InitializeComponent();
this.button1.Command = clearCmd;
this.clearCmd.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt));//使用快捷键
// 指令命令目标
this.button1.CommandTarget = this.textBoxA;
// 创建命令绑定
CommandBinding cb = new CommandBinding();
cb.Command = this.clearCmd;
cb.CanExecute += cb_CanExecute;
cb.Executed += cb_Executed;
this.stackPanel.CommandBindings.Add(cb);
}
void cb_Executed(object sender, ExecutedRoutedEventArgs e)
{
this.textBoxA.Clear();
e.Handled = true;
}
void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (string.IsNullOrEmpty(this.textBoxA.Text))
e.CanExecute = false;
else
e.CanExecute = true;
e.Handled = true;
}
}
}
mvvm command的使用案例的更多相关文章
- MVVM Command Binding: InvokeCommandAction v.s. EventToCommand
This gives you the ability to create a trigger on an event and bind it to an ICommand on the view mo ...
- [Design Pattern] Command Pattern 简单案例
Command Pattern, 即命令模式,把一个命令包裹在一个对象里面,将命令对象传递给命令的执行方,属于行为类的设计模式 下面是命令模式的一个简单案例. Stock 代表被操作的对象.Order ...
- TreeView 中 MVVM Command Binding
<HierarchicalDataTemplate x:Key="TreeNodes" ItemsSource="{Binding Path=Childs,Mode ...
- extJs学习基础5 理解mvvm的一个小案例
今天很是幸运,看到了一位大神的博客,学习了不少的东西.太感谢了.(满满的都是爱啊) 建议去学习这个大神的博客,真心不错. 博客地址:http://blog.csdn.net/column/detail ...
- MVVM With ReactiveCocoa让移动开发更简单
作者:@雷纯锋2011 MVVM是一种软件架构模式,它是 Martin Fowler 的 Presentation Model 的一种变体,最先由微软的架构师 John Gossman 在 2005 ...
- (WPF) 基本题
What is WPF? WPF (Windows Presentation foundation) is a graphical subsystem for displaying user inte ...
- HTML5语义元素
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 文件墙 CFilewall
文件墙 CFilewall 记于 2013-09-26 == @[代码] [C#] []WPF] #### 使用了一些公司的组件和放大,但是不多,可以单独抽取出来 --- 程序结构 - Control ...
- ansible学习(二)
什么是YAML? YAML是一种标记语言.适合用来表达层次结构式的数据结构. YAML的基本组件:清单(短杠——空白字符)和散列表(短杠+空白字符分隔key:value对). Playbook的核心元 ...
随机推荐
- day-8 python自带库实现ID3决策树算法
前一天,我们基于sklearn科学库实现了ID3的决策树程序,本文将基于python自带库实现ID3决策树算法. 一.代码涉及基本知识 1. 为了绘图方便,引入了一个第三方treePlotter模块进 ...
- 测试理论- the conten of test plan
1 testing objects 测试对象 2 testing scope 测试范围 3 testing the frame (?) 4 the environment 5 reason for t ...
- [转]Linux UDP严重丢包问题的解决
测试系统在Linux上的性能发现丢包率极为严重,发210000条数据,丢包达110000之巨,丢包率超过50%.同等情形下Windows上测试,仅丢几条数据.形势严峻,必须解决.考虑可能是因为协议栈B ...
- uva 116 Unidirectional TSP(动态规划,多段图上的最短路)
这道题目并不是很难理解,题目大意就是求从第一列到最后一列的一个字典序最小的最短路,要求不仅输出最短路长度,还要输出字典序最小的路径. 这道题可以利用动态规划求解.状态定义为: cost[i][j] = ...
- PAT 甲级 1042 Shuffling Machine
https://pintia.cn/problem-sets/994805342720868352/problems/994805442671132672 Shuffling is a procedu ...
- SQL 视图 局部变量 全局变量 条件语句 事务 触发器
一.视图 1.视图是一张虚拟表,他所存储的不是实际数据,而是查询语句,但我们可以对视图进行像数据表一样的操作. 2.为什么使用视图呢?我的理解是:1.在远程传输数据时,可以避免过长的查询字符,减少流量 ...
- [剑指Offer] 8.跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. [思路]与斐波那契数列类似 class Solution { public: int jumpF ...
- 【Python】Linux crontab定时任务配置方法(详解)
CRONTAB概念/介绍 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行. cron 系统调度进程. 可以使用它在 ...
- Win10上部署Apollo配置中心
基于Docker在Win10上部署Apollo配置中心 https://www.jianshu.com/p/a1215056ce75 http://nobodyiam.com/2016/07/09/i ...
- [洛谷P4940]Portal2
题目大意:维护两个栈,几个操作: $PUSH\;x\;num:$把$num$压入栈$x$ $POP\;x:$弹出栈$x$栈顶元素 $ADD\;x:$取出两个栈栈顶,把相加值压入栈$x$ $SUB\;x ...