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

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

完成:

html 复制代码
            <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>
csharp 复制代码
        public void Test1(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Txt1 = "改变了";
        }

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

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

csharp 复制代码
    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));
        }
    }
相关推荐
明耀1 天前
WPF TabControl 设置item不能点击
wpf
军训猫猫头1 天前
20.抽卡只有金,带保底(WPF) C#
ui·c#·wpf
明耀1 天前
WPF 设置平均布局 如果隐藏的话,能够自动扩展
wpf
晚安苏州2 天前
WPF DataTemplate 数据模板
wpf
甜甜不吃芥末3 天前
WPF依赖属性详解
wpf
Hat_man_3 天前
WPF制作图片闪烁的自定义控件
wpf
晚安苏州4 天前
WPF Binding 绑定
wpf·wpf binding·wpf 绑定
wangnaisheng4 天前
【WPF】RenderTargetBitmap的使用
wpf
dotent·5 天前
WPF 完美解决改变指示灯的颜色
wpf
orangapple6 天前
WPF 用Vlc.DotNet.Wpf实现视频播放、停止、暂停功能
wpf·音视频