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));
        }
    }
}
相关推荐
麻花201312 小时前
WPF的表格控件 FlexGrid设置行的高度自适应
wpf
挖石油的问天14 小时前
WPF(C#)中的组件1:ItemsControl
前端·c#·wpf
就是有点傻1 天前
C#中面试的常见问题004
面试·职场和发展·c#·wpf
martian6652 天前
C# 开发应用篇——C# 基于WPF实现数据记录导出excel详解
c#·wpf·excel
就是有点傻2 天前
C#中面试的常见问题005
开发语言·面试·c#·wpf
gjhaoiaao2 天前
《白帽子讲Web安全》13-14章
安全·web安全·php·wpf
就是有点傻2 天前
WPF中的Button按钮中的PreviewMouseLeftButtonDown事件和MouseLeftButtonDown的区别
c#·wpf
海的那边-2 天前
WPF 加载页面的三种方式(瞬时加载,延迟加载,异步行为)
wpf
lfw20192 天前
WPF ItemsControl控件
wpf
就是有点傻2 天前
C#中面试的常见问题007
面试·c#·wpf