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>

相关推荐
黎雁·泠崖几秒前
Java入门篇之吃透基础语法(二):变量全解析(进制+数据类型+键盘录入)
java·开发语言·intellij-idea·intellij idea
仙俊红3 分钟前
LeetCode484周赛T4
java
计算机毕设指导618 分钟前
基于微信小程序的丽江市旅游分享系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·旅游
毕设源码-郭学长19 分钟前
【开题答辩全过程】以 基于python电商商城系统为例,包含答辩的问题和答案
开发语言·python
散峰而望22 分钟前
【算法竞赛】栈和 stack
开发语言·数据结构·c++·算法·leetcode·github·推荐算法
Mr -老鬼23 分钟前
Rust 的优雅和其他语言的不同之处
java·开发语言·rust
网安CILLE25 分钟前
PHP四大输出语句
linux·开发语言·python·web安全·网络安全·系统安全·php
weixin_5316518127 分钟前
Rust 的所有权机制
java·开发语言·rust
江公望31 分钟前
QT/QML qmlRegisterType()函数浅谈
开发语言·qt