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));
        }
    }
}
相关推荐
code_shenbing4 小时前
WPF实现打印机控制及打印
wpf
界面开发小八哥1 天前
界面组件DevExpress WPF中文教程:Grid - 如何显示和隐藏列?
wpf·界面控件·devexpress·ui开发·.net9
虚假程序设计1 天前
python用 PythonNet 从 Python 调用 WPF 类库 UI 用XAML
python·ui·wpf
落落落sss1 天前
MongoDB
数据库·windows·redis·mongodb·微服务·wpf
蒋劲豪1 天前
WPF项目暴露WebApi接口;WinForm项目暴露WebApi接口;C#项目暴露WebApi接口;
开发语言·c#·wpf
狮歌~资深攻城狮2 天前
未来已来:HBase的新功能与发展趋势展望
大数据·wpf·hbase
界面开发小八哥3 天前
界面控件DevExpress WPF v24.2新版亮点:支持.NET 9
.net·wpf·界面控件·devexpress·ui开发·用户界面
九鼎科技-Leo4 天前
WPF快速创建DeepSeek本地自己的客户端-基础思路版本
wpf
MasterNeverDown5 天前
WPF 中为 Grid 设置背景图片全解析
大数据·hadoop·wpf
苏克贝塔5 天前
WPF8-常用控件
wpf