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));
        }
    }
}
相关推荐
李高钢14 小时前
【WPF】MVVM 与数据绑定实战:从原理到完整项目
wpf
李高钢20 小时前
【WPF】从 Avalon 到 .NET 10:WPF 平台演进史与特性全览
.net·wpf
李高钢1 天前
WPF MVVM Light 入门:从零到Hello World
c#·wpf
李高钢2 天前
WPF MVVM Light(三):RelayCommand 命令与 Messenger 消息
前端·c#·wpf
SamChan903 天前
FastAPI+Celery+Redis搭建企业级PDF翻译异步任务系统
redis·后端·python·pdf·wpf·fastapi
李高钢3 天前
C# WPF Prism 进阶(三):导航(Navigation)深入
开发语言·c#·wpf
李高钢4 天前
C# WPF 的 Prism 框架入门:核心概念与一个完整示例
开发语言·c#·wpf
SamChan904 天前
用Playwright端到端测试PDF翻译功能:Web自动化测试实战
前端·python·ai·pdf·wpf
Now喔4 天前
WPF动画
wpf
FuckPatience6 天前
WPF 给一个界面上的按钮应用同一种样式
wpf