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. ThreadLocal使用以及使用场景

    2024-06-07 11:52:04       25 阅读
  2. git使用

    2024-06-07 11:52:04       72 阅读
  3. websoket 使用

    2024-06-07 11:52:04       55 阅读
  4. Logstash使用方法

    2024-06-07 11:52:04       66 阅读
  5. Auth使用、缓存

    2024-06-07 11:52:04       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 11:52:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 11:52:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 11:52:04       82 阅读
  4. Python语言-面向对象

    2024-06-07 11:52:04       91 阅读

热门阅读

  1. 算法学习笔记——算法和数据结构简介

    2024-06-07 11:52:04       29 阅读
  2. 解释Servlet过滤器的作用和用法

    2024-06-07 11:52:04       28 阅读
  3. 【WPF编程宝典】第7讲:样式和触发器

    2024-06-07 11:52:04       33 阅读
  4. k8s AIOps

    k8s AIOps

    2024-06-07 11:52:04      25 阅读
  5. 面试 Redis 八股文十问十答第三期

    2024-06-07 11:52:04       33 阅读