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

在wpf 中 用mvvm 的方式 绑定 鼠标事件

在 wpf中, 如果遇到控件的 MouseEnter MouseLeave 属性时, 往往会因为有参数object sender, System.Windows.Input.MouseEventArgs e 很多人选择直接生成属性在后台, 破坏了MVVM, 这其实是不必要的. 我们完全可以用 xmlns:i=“http://schemas.microsoft.com/xaml/behaviors”

完成:

            <TextBlock
                d:Text="设计时文字"
                FontSize="50"
                Text="{Binding Txt1}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseEnter">
                        <i:CallMethodAction MethodName="Test1" TargetObject="{Binding}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </TextBlock>
        public void Test1(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Txt1 = "改变了";
        }

用CallMethodAction 可以绑定到viewmodel中的函数, 并且正确接收鼠标事件 MouseEventArgs

再记录一个 读取Siemens PLC 的通用类

    public class DB10 : INotifyPropertyChanged
    {
        public bool B1
        {
            get => b1;
            set
            {
                if (b1 != value && value == false)
                {
                    RaisePropertyChanged();
                }
                b1 = value;
            }
        }



        public bool B2 { get; set; }
        public bool B3 { get; set; }
        public bool B4 { get; set; }
        public bool B5 { get; set; }
        public bool B6 { get; set; }
        public bool B7 { get; set; }
        public bool B8 { get; set; }
        public bool B9 { get; set; }
        public bool B10 { get; set; }
        public bool B11 { get; set; }
        public bool B12 { get; set; }
        public bool B13 { get; set; }
        public bool B14 { get; set; }
        public bool B15 { get; set; }
        public bool B16 { get; set; }

        public short S1 { get; set; }
        public short S2 { get; set; }
        public short S3 { get; set; }
        public short S4 { get; set; }
        public short S5 { get; set; }
        public short S6 { get; set; }
        public short S7 { get; set; }
        public short S8 { get; set; }
        public short S9 { get; set; }
        public short S10 { get; set; }
        public short S11 { get; set; }
        public short S12 { get; set; }
        public short S13 { get; set; }
        public short S14 { get; set; }
        public short S15 { get; set; }
        public short S16 { get; set; }

        public float F1 { get; set; }
        public float F2 { get; set; }
        public float F3 { get; set; }
        public float F4 { get; set; }
        public float F5 { get; set; }
        public float F6 { get; set; }
        public float F7 { get; set; }
        public float F8 { get; set; }
        public float F9 { get; set; }
        public float F10 { get; set; }
        public float F11 { get; set; }
        public float F12 { get; set; }
        public float F13 { get; set; }
        public float F14 { get; set; }
        public float F15 { get; set; }
        public float F16 { get; set; }

        /// <summary>
        /// 图纸
        /// </summary>
        [S7String(S7StringType.S7String, 50)]
        public string Str1 { get; set; } = string.Empty;

        /// <summary>
        /// 图纸
        /// </summary>
        [S7String(S7StringType.S7String, 50)]
        public string Str2 { get; set; } = string.Empty;

        /// <summary>
        /// 图纸
        /// </summary>
        [S7String(S7StringType.S7String, 50)]
        public string Str3 { get; set; } = string.Empty;



        public event PropertyChangedEventHandler? PropertyChanged;


        private bool b1;
        private void RaisePropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

http://www.kler.cn/news/353752.html

相关文章:

  • Java笔试03
  • Linux 线程概念及线程控制
  • 系统缺失mfc140.dll的修复方法,有效修复错误mfc140.dll详细步骤
  • VLAN概述
  • 阻塞I/O与非阻塞I/O
  • 408算法题leetcode--第36天
  • 从HCI和空口分析HFP通话和eSCO建立
  • 【进阶OpenCV】 (18)-- Dlib库 --人脸关键点定位
  • 如何高效解锁业务数据价值:多云时代应该怎么构建新一代数据平台架构
  • Allegro 小技巧
  • 二叉树与堆讲解
  • unity动态批处理
  • Spring Boot知识管理系统:安全与合规性
  • linux红帽基础命令
  • 【力扣热题100】3194. 最小元素和最大元素的最小平均值【Java】
  • mysql--数据表的操作
  • AI Weekly#1:过去一周重要的AI资讯汇总
  • 基于知识图谱的宁夏非遗问答系统
  • URL路径以及Tomcat本身引入的jar包会导致的 SpringMVC项目 404问题、Tomcat调试日志的开启及总结
  • Spring Boot: 构建高效中小型医院网站