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));
        }
    }
}
相关推荐
qiangshang9901269 小时前
WPF+MVVM入门学习
学习·wpf
DASXSDW9 小时前
Abp vNext-事件总线使用实现及解析
ui·wpf
纸照片9 小时前
【邪修玩法】如何在WPF中开放 RESTful API 服务
后端·wpf·restful
啊丢_11 小时前
WPF基本布局容器与控件
wpf
c#上位机14 小时前
wpf之RelativeSource用法总结
c#·wpf
玖笙&4 天前
✨WPF编程基础【2.1】布局原则
c++·wpf·visual studio
玖笙&4 天前
✨WPF编程基础【2.2】:布局面板实战
c++·wpf·visual studio
SEO-狼术4 天前
.NET WPF 数据编辑器集合提供列表框控件
.net·wpf
FuckPatience8 天前
WPF 具有跨线程功能的UI元素
wpf
诗仙&李白8 天前
HEFrame.WpfUI :一个现代化的 开源 WPF UI库
ui·开源·wpf