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>

相关推荐
我爱娃哈哈8 分钟前
SpringBoot + XXL-JOB + Quartz:任务调度双引擎选型与高可用调度平台搭建
java·spring boot·后端
赛恩斯12 分钟前
kotlin 为什么可以在没有kotlin 环境的安卓系统上运行的
android·开发语言·kotlin
steem_ding13 分钟前
net.core 调优指南
开发语言·php
小宇的天下15 分钟前
Synopsys Technology File and Routing Rules Reference Manual (1)
java·服务器·前端
Coder_Boy_26 分钟前
基于SpringAI的在线考试系统-AI智能化拓展
java·大数据·人工智能·spring boot
n***333529 分钟前
TCP/IP协议栈深度解析技术文章大纲
java·spring boot
奋进的芋圆33 分钟前
Java 线程池深度指南(JDK 17+)
java
蓁蓁啊1 小时前
GCC 头文件搜索路径:-I vs -idirafter 深度解析
java·前端·javascript·嵌入式硬件·物联网
龙门吹雪1 小时前
GO 语言处理多个布尔选项的实现方案
开发语言·后端·golang·布尔选项·标识位