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

news/2024/10/16 19:25:57 标签: wpf

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.niftyadmin.cn/n/5708427.html

相关文章

【C语言】数据类型

C语言常用数据类型 int 整型 4字节float 浮点型 4字节double 双精度浮点类型 8字节char 字符型 1字节构造类型&#xff1a;数组&#xff08;多个相同类型的变量组合&#xff09;构造类型&#xff1a;结构体&#xff08;多个不同类型的变量组合&#xff09; #include <stdi…

机器学习:opencv--人脸检测以及微笑检测

目录 前言 一、人脸检测的原理 1.特征提取 2.分类器 二、代码实现 1.图片预处理 2.加载分类器 3.进行人脸识别 4.标注人脸及显示 三、微笑检测 前言 人脸检测是计算机视觉中的一个重要任务&#xff0c;旨在自动识别图像或视频中的人脸。它可以用于多种应用&#xff0…

mysql学习教程,从入门到精通,SQL导入数据(44)

1.SQL 导出数据 以下是一个关于如何使用 SQL 导出数据的示例。这个示例将涵盖从一个关系数据库管理系统&#xff08;如 MySQL&#xff09;中导出数据到 CSV 文件的基本步骤。 1.1、前提条件 你已经安装并配置好了 MySQL 数据库。你有访问数据库的权限。你知道要导出的表名。…

微信小程序中的文件查看方法

获得后缀名判断类型,如果是图片用ex.previewImage(),如果是视频,用uni.previewMedia(),如果是word文档这些的,用 uni.downloadFile来下载资源后用 uni.saveFile来保存到本地,uni.openDocument来打开新的网页,如果打不开的话则返回说到PC端去打开 const lookFile (url) > {l…

MySQL中FIND_IN_SET(),IN()和LIKE区别

在 MySQL 中&#xff0c; FIND_IN_SET() 和 LIKE 都可以用于字符串的匹配查找&#xff0c;但它们有以下不同&#xff1a; 一、语法及功能 1. FIND_IN_SET(str,strlist) &#xff1a; 用于在以逗号分隔的字符串列表中查找特定字符串&#xff0c;并返回其位置。如果未找到则返…

DOS时代渐行渐远,而国产新型平台,却在悄然换代

可能就是有点儿念旧吧&#xff0c;没事干的时候&#xff0c;就爱把那些DOS年代的经典软件找出来摆弄摆弄。 虽说它们现在都是老古董了&#xff0c;但在咱们70后、80后的心里头&#xff0c;这些当年闪闪发光的软件宝贝&#xff0c;还是有着独一无二的位置&#xff0c;谁也替代不…

60个java常用的代码(能够帮助您更好地理解Java编程)

以下是60个常用的Java代码片段&#xff0c;涵盖了基础语法、常用类和一些实用功能&#xff1a; 基础语法 Hello World public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } 变量声明 int number…

『网络游戏』客户端使用PESorket发送消息到服务器【14】

上一章服务器已经完成使用PESorket 现在我们将其导出在客户端中使用 生成成功后复制 粘贴到Unity项目中 进入Assets文件夹 粘贴两个.dll 创建脚本:ClientSession.cs 编写脚本: ClientSession.cs 编写脚本:GameStart.cs 将GameStart.cs脚本绑定在摄像机上 运行服务器 运行客户端…