主界面如下:

前台代码:

<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的使用案例的更多相关文章

  1. 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 ...

  2. [Design Pattern] Command Pattern 简单案例

    Command Pattern, 即命令模式,把一个命令包裹在一个对象里面,将命令对象传递给命令的执行方,属于行为类的设计模式 下面是命令模式的一个简单案例. Stock 代表被操作的对象.Order ...

  3. TreeView 中 MVVM Command Binding

    <HierarchicalDataTemplate x:Key="TreeNodes" ItemsSource="{Binding Path=Childs,Mode ...

  4. extJs学习基础5 理解mvvm的一个小案例

    今天很是幸运,看到了一位大神的博客,学习了不少的东西.太感谢了.(满满的都是爱啊) 建议去学习这个大神的博客,真心不错. 博客地址:http://blog.csdn.net/column/detail ...

  5. MVVM With ReactiveCocoa让移动开发更简单

    作者:@雷纯锋2011 MVVM是一种软件架构模式,它是 Martin Fowler 的 Presentation Model 的一种变体,最先由微软的架构师 John Gossman 在 2005 ...

  6. (WPF) 基本题

    What is WPF? WPF (Windows Presentation foundation) is a graphical subsystem for displaying user inte ...

  7. HTML5语义元素

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 文件墙 CFilewall

    文件墙 CFilewall 记于 2013-09-26 == @[代码] [C#] []WPF] #### 使用了一些公司的组件和放大,但是不多,可以单独抽取出来 --- 程序结构 - Control ...

  9. ansible学习(二)

    什么是YAML? YAML是一种标记语言.适合用来表达层次结构式的数据结构. YAML的基本组件:清单(短杠——空白字符)和散列表(短杠+空白字符分隔key:value对). Playbook的核心元 ...

随机推荐

  1. jqprint导入jqgrid表格时,内容溢出的原因以及解决方法

    jqprint在导入表格的时候,会将原表格的样式全部拉过来,所以说原表格(如jqgrid的表格)的内容在有滚动条的时候,必须得将宽度设置为100%(等百分比的宽度),不能设置成固定宽度,不然表格内容会 ...

  2. 在Android Studio中创建(或添加)第一个Hello World应用程序

    下面我们将使用Android Studio创建第一个简单的Hello World应用程序. 1.打开Android Studio,加载画面如下图所示:   2.选择”Start a new Andro ...

  3. 查看ClassLoader载入了哪些类?

    在执行jar时加上-verbose:class java  -verbose:class -Xms1G -Xmx2G -jar xx.jar 必要时还可以使用 >log.txt 将输出输入到文本 ...

  4. 编程练习:寻找发帖"水王"扩展问题一

    回顾 寻找发帖水王的问题总结起来就是在一个数组中某一个元素出现次数超过了数组长度的一半,那么可以很顺利的找到这个元素,实现见"编程练习:寻找发帖水王" 扩展 上面的问题中,强调了某 ...

  5. Java开发环境配置时的dt.jar与tools.jar是什么(转载)

    你了解dt.jar吗 很多人在初学Java的时候,都要配置环境变量.在配置CLASSPATH的时候,都会加上一个当前目录.,还有两个jar:dt.jar和tools.jar.其实好多人都不了解这两个j ...

  6. JAVA第八周课堂笔记

  7. JSP表单提交出现中文乱码的解决方法

    1)post方式 在servlet的doGet( )  doPost( )  中增加以下代码: response.setContentType("text/html;charset=utf- ...

  8. BZOJ4318 OSU!(动态规划+概率期望)

    设f[i][0/1]为考虑前i位,第i位为0/1时的期望得分(乘以是0/1的概率).暴力转移显然.前缀和优化即可. 但是这个前缀和精度无法承受,动不动就nan. 考虑增加一位的贡献.若之前后缀1的个数 ...

  9. pmap用法小计

    By francis_hao    Aug 4,2017   pmap-报告进程的内存映射.   概要 pmap [options] pid [...]   描述 pmap命令用来报告一个或多个进程的 ...

  10. Mysql History list length 值太大引起的问题

    1. 环境 Mysql 主从 Mysql版本:5.1.49-log 系统:Red Hat Enterprise Linux Server release 5.4  64bit 2. 表面现象 数据库操 ...