wpf INotifyPropertyChanged

模版1:

cs 复制代码
public abstract class ViewModelBase : INotifyPropertyChanged  
{  
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)  
    {  
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
    }  
  
    // ... 其他代码 ...  
}  
  
public class MyViewModel : ViewModelBase  
{  
    private string _myProperty;  
    public string MyProperty  
    {  
        get => _myProperty;  
        set  
        {  
            if (_myProperty == value) return;  
            _myProperty = value;  
            OnPropertyChanged(); // 这里不需要指定属性名,因为使用了CallerMemberName特性  
        }  
    }  
}

模版2:

cs 复制代码
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace zl_screenplayer
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<DotViewModel> _dots;
        private int _currentIndex;

        public ObservableCollection<DotViewModel> Dots
        {
            get { return _dots; }
            set { _dots = value; OnPropertyChanged(); }
        }

        public int CurrentIndex
        {
            get { return _currentIndex; }
            set
            {
                if (_currentIndex != value)
                {
                    _dots[_currentIndex].IsActive = false;
                    _currentIndex = value;
                    _dots[_currentIndex].IsActive = true;
                    OnPropertyChanged();
                }
            }
        }

        public MainViewModel(int Count,int InitIndex)
        {
            var list = new ObservableCollection<DotViewModel>();
            for (int i = 0; i < Count; i++)
            {
                list.Add(new DotViewModel { IsActive = (i == InitIndex ? true : false) });
            }
            Dots = list;
            _currentIndex = InitIndex;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class DotViewModel : INotifyPropertyChanged
    {
        private bool _isActive;

        public bool IsActive
        {
            get { return _isActive; }
            set { _isActive = value; OnPropertyChanged(); }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
相关推荐
大雄野比2 小时前
Rubyer-WPF:打造优雅、精致的 WPF 用户界面
ui·wpf
吉量*6 小时前
WPF系列九:图形控件EllipseGeometry
wpf·上位机·绘图·ellipsegeometry
步、步、为营6 小时前
WPF设计时特性加速界面设计
wpf
吉量*6 小时前
WPF系列八:图形控件Path
wpf·绘图·path·图形
bug终结者10246 小时前
WPF连接USB相机,拍照,视频 示例
数码相机·c#·wpf
坐井观老天1 天前
使用WPF在C#中制作下载按钮
开发语言·c#·wpf
wangnaisheng2 天前
【WPF】使用BitmapImage给Image的Source赋值,并释放原占用资源,避免删除原文件时导致程序崩溃
c#·wpf
还是叫明2 天前
WPF中RenderTargetBitmap问题解决
wpf
雾削木2 天前
C# WPF Material DesignThemes 5.0 命名规则改变后导致找不到资源
开发语言·c#·wpf
流浪阿丁2 天前
WPF页面中将一个控件的宽度绑定到其父级用户控件的实际宽度
wpf