在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));
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值