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>

相关推荐
鬼火儿3 分钟前
SpringBoot】Spring Boot 项目的打包配置
java·后端
NON-JUDGMENTAL16 分钟前
Tomcat 新手避坑指南:环境配置 + 启动问题 + 乱码解决全流程
java·tomcat
大佬,救命!!!1 小时前
C++多线程同步与互斥
开发语言·c++·学习笔记·多线程·互斥锁·同步与互斥·死锁和避免策略
chxii1 小时前
Maven 详解(上)
java·maven
李少兄1 小时前
IntelliJ IDEA 远程调试(Remote Debugging)教程
java·ide·intellij-idea
Kuo-Teng1 小时前
Leetcode438. 找到字符串中所有字母异位词
java·算法·leetcode
毕设小屋vx ylw2824261 小时前
Java开发、Java Web应用、前端技术及Vue项目
java·前端·vue.js
TDengine (老段)1 小时前
TDengine 字符串函数 CHAR 用户手册
java·大数据·数据库·物联网·时序数据库·tdengine·涛思数据
赵文宇(温玉)1 小时前
构建内网离线的“github.com“,完美解决内网Go开发依赖
开发语言·golang·github
float_com1 小时前
【java基础语法】------ 数组
java