当前位置: 首页 > 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" />
http://www.dtcms.com/a/89542.html

相关文章:

  • 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
  • 【附代码】【MILP建模】3D装箱问题(3D-Bin Packing Problem)
  • 冠珠瓷砖×郭培:当东方美学邂逅匠心工艺,高定精神如何重塑品质生活?
  • 企业在人工智能创新与安全之间走钢丝
  • TDengine 中的系统信息统计
  • oracle查询归档日志使用量
  • 阳台光伏新守护者:电流传感器助力安全发电
  • 【开源宝藏】用 JavaScript 手写一个丝滑的打字机动画效果
  • 软件确认测试注意事项和工具分享,确认测试软件测评中心有哪些?
  • 【报错】 /root/anaconda3/conda.exe: cannot execute binary file: Exec format error
  • 可变形交互注意力模块(DIA-Module)及代码详解