C# 扩展集合ObservableCollection使集合在添加、删除、值变更后触发事件

目录

概述

集合扩展

事件订阅

效果演示


概述

     ObservableCollection继承了INotifyPropertyChanged接口,在属性变更时可以通知界面,当我把ObservableCollection集合绑定到界面的DataGrid后,我希望在界面修改表格数值后,可以触发一个 事件来验证我界面设定数据的有效性,但是对于集合的添加、删除只会触发集合的get属性,值重置不会触发集合的get、set属性,这时候我们就需要扩展ObservableCollection集合.

集合扩展

代码如下:重写OnCollectionChanged方法,使得集合改变(增添、删除、改变)时拥有属性变更事件

using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using DevExpress.Xpo;

namespace Caliburn.Micro.Hello
{
    public class ItemsChangeObservableCollection<T> :
            System.Collections.ObjectModel.ObservableCollection<T> where T : INotifyPropertyChanged
    {
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                RegisterPropertyChanged(e.NewItems);
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                UnRegisterPropertyChanged(e.OldItems);
            }
            else if (e.Action == NotifyCollectionChangedAction.Replace)
            {
                UnRegisterPropertyChanged(e.OldItems);
                RegisterPropertyChanged(e.NewItems);
            }

            base.OnCollectionChanged(e);
        }

        protected override void ClearItems()
{
            UnRegisterPropertyChanged(this);
            base.ClearItems();
        }

        private void RegisterPropertyChanged(IList items)
{
            foreach (INotifyPropertyChanged item in items)
            {
                if (item != null)
                {
                    item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
                }
            }
        }

        private void UnRegisterPropertyChanged(IList items)
{
            foreach (INotifyPropertyChanged item in items)
            {
                if (item != null)
                {
                    item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
                }
            }
        }

        private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
            //launch an event Reset with name of property changed
            base.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
    }
}

事件订阅

可以用如下方法订阅事件:​​​​​​​

this.StudentList.CollectionChanged += StudentList_OnCollectionChanged;
或
StudentList.CollectionChanged += new NotifyCollectionChangedEventHandler(StudentList_OnCollectionChanged);
 

事件方法:​​​​​​​

public void StudentList_OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            MessageBox.Show("当前触发的事件是:"+ e.Action.ToString());
        }

集合定义:​​​​​​​

private ItemsChangeObservableCollection<Students> studentList;
        public ItemsChangeObservableCollection<Students> StudentList
        {
            get
            {
                return studentList;
            }
            set
            {
                studentList = value;
            }
        }

效果演示

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值