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

发布于:2024-10-16 ⋅ 阅读:(16) ⋅ 点赞:(0)

在 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));
        }
    }