zoukankan      html  css  js  c++  java
  • WPF 绑定集合 根据集合个数改变样式 INotifyCollectionChanged

     问题:当前ListBox Items 绑定 集合数据源ListA时候;ListA集合数据源中存在另外一个集合ListB,当更改或往ListB集合中添加数据的时候,通知改变?

    实体类继承 INotifyCollectionChanged 即可实现:

    BaseViewModel:

    public class BaseViewModel : INotifyPropertyChanged, INotifyCollectionChanged, IDisposable
        {
            public event PropertyChangedEventHandler PropertyChanged;
            public event NotifyCollectionChangedEventHandler CollectionChanged;
    
            public void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            public virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
            {
                if (CollectionChanged != null)
                {
                    CollectionChanged(this, e);
                }
            }
    
            public void Dispose()
            {
                this.OnDispose();
            }
    
            protected void OnDispose() { }
    
            public void OnCollectionChanged()
            {
            }
        }
    View Code

    ViewModel:

    public class ViewModel : BaseViewModel
        {
            private List<Person> _lp = null;
            private RelayCommand _addCommand, _removeCommand;
    
            public ViewModel()
            {
                LP = new List<Person>();
                LP.Add(new Person(1, "aaa"));
                LP.Add(new Person(2, "bbb"));
            }
    
            public List<Person> LP
            {
                get { return _lp; }
                set { _lp = value; }
            }
    
            public ICommand AddCommand
            {
                get
                {
                    if (_addCommand == null)
                    { _addCommand = new RelayCommand(param => this.Add(), param => this.CanAdd); }
                    return _addCommand;
                }
            }
    
            public bool CanAdd
            { get { return true; } }
    
            public void Add()
            {
                Person ps = new Person(3, "ccc");
                LP.Add(ps);
                CollectionChanged += new NotifyCollectionChangedEventHandler(List_CollectionChanged);
                OnPropertyChanged("LP");
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, LP[0], 0));
            }
    
            public ICommand RemoveCommand
            {
                get
                {
                    if (_removeCommand == null)
                    { _removeCommand = new RelayCommand(param => this.Remove(), param => this.CanRemove); }
                    return _removeCommand;
                }
            }
    
            public bool CanRemove
            { get { return true; } }
    
            public void Remove()
            {
                LP.RemoveAt(0);
                CollectionChanged += new NotifyCollectionChangedEventHandler(List_CollectionChanged);
                OnPropertyChanged("LP");
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, LP[0], 0));
            }
    
            private void List_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
            {
                // ??????????
            }
        }
    View Code

    完美解决!!

    https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/1b55492b-e9ca-4610-a40d-107d64c8ea9f/inotifycollectionchanged-on-listt

  • 相关阅读:
    Android之旅 自我图示总结四大组件
    解决.NET 调用JAVA WEBService服务中文乱码问题
    【itercast OSI 七层网络模型 学习笔记】Layer 1 物理层
    用SDL库播放yuy2 Packed mode
    uva539 卡坦岛 简单回溯!
    Zookeeper CLI
    在安装ZooKeeper之前,请确保你的系统是在以下任一操作系统上运行
    Zookeeper leader选举
    Zookeeper 工作流
    ZooKeeper的架构
  • 原文地址:https://www.cnblogs.com/tianciliangen/p/4806722.html
Copyright © 2011-2022 走看看