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 小时前
springboot canche 无法避免Null key错误, Null key returned for cache operation
java·开发语言·spring boot
毕设源码-朱学姐1 小时前
【开题答辩全过程】以 基于SpringBoot的中医理疗就诊系统为例,包含答辩的问题和答案
java·spring boot·后端
2201_757830875 小时前
全局异常处理器
java
知远同学6 小时前
Anaconda的安装使用(为python管理虚拟环境)
开发语言·python
小徐Chao努力6 小时前
【Langchain4j-Java AI开发】09-Agent智能体工作流
java·开发语言·人工智能
CoderCodingNo6 小时前
【GESP】C++五级真题(贪心和剪枝思想) luogu-B3930 [GESP202312 五级] 烹饪问题
开发语言·c++·剪枝
Coder_Boy_6 小时前
SpringAI与LangChain4j的智能应用-(理论篇3)
java·人工智能·spring boot·langchain
kylezhao20196 小时前
第1章:第一节 开发环境搭建(工控场景最优配置)
开发语言·c#
啃火龙果的兔子6 小时前
JavaScript 中的 Symbol 特性详解
开发语言·javascript·ecmascript
Coder_Boy_7 小时前
基于SpringAI的智能平台基座开发-(六)
java·数据库·人工智能·spring·langchain·langchain4j