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>

相关推荐
0wioiw04 分钟前
Python基础(吃洋葱小游戏)
开发语言·python·pygame
知其然亦知其所以然9 分钟前
JVM社招面试题:队列和栈是什么?有什么区别?我在面试现场讲了个故事…
java·后端·面试
栗子~~11 分钟前
Python实战- Milvus 向量库 使用相关方法demo
开发语言·python·milvus
狐凄14 分钟前
Python实例题:基于 Flask 的在线聊天系统
开发语言·python
狐凄14 分钟前
Python实例题:基于 Flask 的任务管理系统
开发语言·python
harmful_sheep16 分钟前
Spring 为何需要三级缓存解决循环依赖,而不是二级缓存
java·spring·缓存
星辰大海的精灵18 分钟前
如何确保全球数据管道中的跨时区数据完整性和一致性
java·后端·架构
大大。21 分钟前
van-tabbar-item选中active数据变了,图标没变
java·服务器·前端
nc_kai24 分钟前
Flutter 之 每日翻译 PreferredSizeWidget
java·前端·flutter
weixin_4471035824 分钟前
Wpf布局之StackPanel!
wpf