zoukankan      html  css  js  c++  java
  • ICommand接口

    WPF 中的命令是通过实现 ICommand 接口创建的。ICommand 的 WPF 实现是 RoutedCommand 类。

      WPF 中的主要输入源是鼠标、键盘、墨迹和路由命令。更加面向设备的输入使用 RoutedEvent 来通知应用程序页中的对象已发生了输入事件。

    ICommand接口只要是用于绑定Button等的点击事件,当数据改变的时候,会做出一些相应的通知,

    当继承ICommand接口后,下面的方法则需要实现,它们之间的关系如图所示:

    定义一个类实现ICommand接口

       public class DelegateCommand : ICommand
        {
            public Action<object> ExecuteAction { get; set; }
            public Func<object,bool> CanExecuteFunc { get; set; }
    
            public event EventHandler CanExecuteChanged;
    
            public bool CanExecute(object parameter)
            {
                if (CanExecuteFunc == null)
                    return true;
                return
                    CanExecuteFunc(parameter);
            }
    
            public void Execute(object parameter)
            {
                if (ExecuteAction == null)
                    return;
                ExecuteAction(parameter);
            }
        }

    在调用类中添加方法

    public MainWindow()
            {
                InitializeComponent();
    
                AddCommand = new DelegateCommand();
                AddCommand.ExecuteAction = (para)=>
                {
                    MessageBox.Show("我是按钮");
                };
            }
            public DelegateCommand AddCommand { get; set; }

    在vm中command的绑定代码

    <Button Content="点击我" Command="{Binding AddCommand}" />
  • 相关阅读:
    实验五 shell脚本编程
    实验四 Linux系统C语言开发环境学习
    实验三 Linux系统用户管理及VIM配置
    实验二 Linux系统简单文件操作命令
    实验一 Linux系统与应用准备
    实验八 进程间通信
    实验七 信号
    实验六 进程基础
    实验五 shell脚本编程
    实验四 Linux系统搭建C语言编程环境
  • 原文地址:https://www.cnblogs.com/lunawzh/p/4806178.html
Copyright © 2011-2022 走看看