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>

相关推荐
jiayong231 分钟前
Spring 框架完全指南
java·后端·spring
高山上有一只小老虎1 分钟前
小红的正整数计数
java·算法
AnAnCode2 分钟前
【时间轮算法-实战】Java基于Netty的 `HashedWheelTimer`快速搭建时间轮算法系统
java·开发语言·算法·时间轮算法
liu****12 分钟前
12.C语言内存相关函数
c语言·开发语言·数据结构·c++·算法
while(1){yan}18 分钟前
JAVA单例模式
java·单例模式
5008422 分钟前
鸿蒙 Flutter 权限管理进阶:动态权限、权限组、兼容处理与用户引导
flutter·华为·架构·wpf·开源鸿蒙
没有bug.的程序员26 分钟前
Async Profiler:最精准的火焰图工具
java·jvm·spring·对象分配·async profiler
金士顿32 分钟前
Ethercat耦合器添加的IO导出xml 初始化IO参数
android·xml·java
7哥♡ۣۖᝰꫛꫀꪝۣℋ38 分钟前
Spring WebMVC及常用注释
java·数据库·spring
曹牧40 分钟前
C#:Dictionary类型数组
java·开发语言·c#