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>

相关推荐
科雷软件测试2 小时前
Python中itertools.product:快速生成笛卡尔积
开发语言·python
OOJO3 小时前
c++---list介绍
c语言·开发语言·数据结构·c++·算法·list
笨笨饿5 小时前
29_Z变换在工程中的实际意义
c语言·开发语言·人工智能·单片机·mcu·算法·机器人
艾为电子5 小时前
【技术帖】让接口不再短命:艾为 C-Shielding™ Type-C智能水汽防护技术解析
c语言·开发语言
棉花骑士6 小时前
【AI Agent】面向 Java 工程师的Claude Code Harness 学习指南
java·开发语言
IGAn CTOU6 小时前
PHP使用Redis实战实录2:Redis扩展方法和PHP连接Redis的多种方案
开发语言·redis·php
爱敲代码的小鱼6 小时前
springboot(2)从基础到项目创建:
java·spring boot·spring
环黄金线HHJX.6 小时前
TSE框架配置与部署详解
开发语言·python
Vfw3VsDKo6 小时前
Maui 实践:Go 接口以类型之名,给 runtime 传递方法参数
开发语言·后端·golang
Pyeako7 小时前
PyQt5 + PaddleOCR实战:打造桌面级实时文字识别工具
开发语言·人工智能·python·qt·paddleocr·pyqt5