WPF Command 的使用

一、Command类的创建 >> 构造函数方法中传入了一个委托

public class MyCommand : ICommand

{

public readonly Action _action;

public MyCommand(Action action) {

this._action = action;

}

public event EventHandler CanExecuteChanged;

public bool CanExecute(object parameter)

{

return true;

}

public void Execute(object parameter)

{

_action();

}

}

二、command在viewmodel中的使用

public class MainViewModel

{

public MyCommand myCommand { get; set; }

public MainViewModel() {

myCommand = new MyCommand(Show);

}

public void Show()

{

MessageBox.Show("你点击了我!");

}

}

三、在页面控件button 中调用

<Button Command="{Binding myCommand}">点击</Button>

相关推荐
笨笨马甲1 天前
Qt QSS使用指南
开发语言·qt
cur1es1 天前
【JVM类加载&双亲委派模型&垃圾回收机制】
java·jvm·gc·垃圾回收·类加载·双亲委派模型
Mr.朱鹏1 天前
JVM-GC垃圾回收案例
java·jvm·spring boot·算法·spring·spring cloud·java-ee
焦糖玛奇朵婷1 天前
实测扭蛋机小程序:开发简单,互动有趣
java·大数据·程序人生·小程序·软件需求
Nan_Shu_6141 天前
学习: 尚硅谷Java项目之小谷充电宝(3)
java·后端·学习
wzqllwy1 天前
8 大经典排序算法(Java 实现):原理 + Demo + 核心分析
java·算法·排序算法
智能工业品检测-奇妙智能1 天前
AIFlowy如何实现与现有Spring Boot项目的无缝集成?
java·spring boot·后端
We་ct1 天前
LeetCode 77. 组合:DFS回溯+剪枝,高效求解组合问题
开发语言·前端·算法·leetcode·typescript·深度优先·剪枝
從南走到北1 天前
JAVA无人共享无人健身房物联网结合系统源码支持小程序+公众号+APP+H5
java·物联网·小程序
格林威1 天前
工业相机图像高速存储(C#版):内存映射文件方法,附Basler相机C#实战代码!
开发语言·人工智能·数码相机·c#·机器视觉·工业相机·堡盟相机