silverlight-简单ObservableDictionary

本文介绍了一个自定义实现的ObservableDictionary类,该类继承自Dictionary并实现了INotifyCollectionChanged接口,以便在集合发生变化时通知UI进行更新。文章提供了完整的源代码,并涵盖了添加、删除和替换元素时触发的不同类型的NotifyCollectionChangedAction。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

忽然觉得ObservableDictionary好像很有用,只是sl里好像没有,算了,自己写一个吧,bug未知,性能未知。

    public class ObservableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, INotifyCollectionChanged
    {
        public event NotifyCollectionChangedEventHandler CollectionChanged;

        private int _index;

        public new TValue this[TKey key]
        {
            get { return this.GetValue(key); }
            set { this.SetValue(key, value); }
        }

        public new void Add(TKey key, TValue value)
        {
            base.Add(key, value);
            this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, this.FindPair(key), _index));
        }

        public new void Clear()
        {
            base.Clear();
            this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public new bool Remove(TKey key)
        {
            var pair = this.FindPair(key);
            if (base.Remove(key))
            {
                this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, pair, _index));
                return true;
            }
            return false;
        }

        private TValue GetValue(TKey key)
        {
            if (ContainsKey(key))
                return base[key];
            else
                return default(TValue);
        }

        private void SetValue(TKey key, TValue value)
        {
            if (ContainsKey(key))
            {
                base[key] = value;
                var pair = this.FindPair(key);
                var index = _index;
                this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, pair, pair, index));
            }
            else
            {
                this.Add(key, value);
            }
        }

        private KeyValuePair<TKey, TValue> FindPair(TKey key)
        {
            _index = 0;
            foreach (var pair in this)
            {
                if (pair.Key.Equals(key))
                    return pair;

                _index++;
            }
            return default(KeyValuePair<TKey, TValue>);
        }

        private void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (this.CollectionChanged != null)
            {
                this.CollectionChanged(this, e);
            }
        }
    }

转载于:https://www.cnblogs.com/HalfwayMonk/archive/2011/01/05/1926683.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值