当前位置: 首页 > news >正文

【WPF】MVVM模式实现数据绑定以及Command事件绑定

1.引用类

using System.ComponentModel

2.创建Command自定义类

 public class DelegateCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            if (CanExecuteFunc == null)
                return true;
            return this.CanExecuteFunc(parameter);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (ExecuteAction == null)
            {
                return;
            }
            this.ExecuteAction(parameter);
        }

        public Action<object> ExecuteAction { get; set; }
        public Func<object, bool> CanExecuteFunc { get; set; }

    }

3.ViewModel层实现数据绑定、Command事件点击

public class MainViewModel : INotifyPropertyChanged
{
        public event PropertyChangedEventHandler PropertyChanged;
         public MainViewModel()
        {
          #必需初始化时就加载才生效
            this.SelectFileNameCommand = new DelegateCommand();
            this.SelectFileNameCommand.ExecuteAction = new Action<object>(this.SelectFileName);
        }
        
        /// <summary>
        /// 当前时间
        /// </summary>
        private string currentTimer;
        public string CurrentTimer
        {
            get
            {
                return currentTimer;
            }
            set
            {
                currentTimer = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CurrentTimer"));
                }
            }
        }

        public DelegateCommand DoubleClickNotifyCommand { get; set; }
        private void DoubleClickNotify(object parameter)
        {
            
        }

}

4.界面按钮绑定事件

<Button
   Margin="0,0,5,0"
    Command="{Binding DoubleClickNotifyCommand }"
    Content="{DynamicResource Setting_File}"
    FontSize="20"
    Foreground="White" />

相关文章:

  • python工厂模式
  • ML29_先验概率与边缘概率的对比
  • 【数据库相关MySql、Redis、MongoDB】
  • 【Git】--- Git远程操作 标签管理
  • 从零开始:使用Luatools工具高效烧录Air780EPM核心板项目的完整指南
  • PHP在2025年的新趋势与应用
  • 投sci论文自己查重方法
  • 拓展知识五:魔术方法
  • DIVA-GIS:一个免费的GIS分析小软件
  • C语言位操作符详解
  • 【无标题】大亚湾文化体育场多美啊。
  • python处理音频相关的库
  • 【工程记录】QwQ-32b 8bit量化部署教程(vLLM | 缓解复读)
  • 2.27省选模拟赛补题记录:直径(容斥,树形dp,换根dp)
  • Java 线程池全面解析
  • vue-将组件内容导出为Word文档-docx
  • StarRocks数据导入
  • 漏洞挖掘 --- Ollama未授权访问
  • 【区块链安全 | 第三篇】主流公链以太坊运行机制
  • PAT甲级(Advanced Level) Practice 1028 List Sorting
  • 郑州 网站建设公司/武汉seo网站推广培训
  • 网站大全app下载/今日头条武汉最新消息
  • 怎么在ps做网站首页/google推广专员招聘
  • 网站设计 收费/全球网站流量查询
  • 如何做照片ppt模板下载网站/深圳百度快速排名优化
  • 做那事的网站/百度网站