代码之家  ›  专栏  ›  技术社区  ›  Greg

WPF-如何使用键(如字典)实现observeCollection<K,T>?

  •  8
  • Greg  · 技术社区  · 14 年前

    你能建议一下可以用来提供这样一个可观察集合的代码吗?目标是拥有一个类似字典的结构,我可以从WPF绑定到它。

    谢谢

    4 回复  |  直到 14 年前
        1
  •  6
  •   Gabe Timothy Khouri    14 年前

    Someone already made it . 我还没试过,但没什么可输的。

        2
  •  2
  •   Stevy    5 年前

    var collection = new ObservableCollection<KeyValuePair<TKey, TValue>>();

    您应该能够通过以下方式解决此问题:

    collection.First(x => x.Key == *your key value*)

        3
  •  1
  •   Steve Ellinger    14 年前

    创建实现IDictionary、INotifyCollectionChanged和INotifyPropertyChanged接口的类。该类将有一个Dictionary实例,用于实现IDictionary(下面将其中一个Add方法作为示例进行编码)。INotifyCollectionChanged和inotifyPropertyChanged都要求存在事件,这些事件应该在包装函数中的适当点激发(同样,请参阅下面的Add方法以获取示例)

    class ObservableDictionary<TKey, TValue> : IDictionary, INotifyCollectionChanged, INotifyPropertyChanged
    {
        private Dictionary<TKey, TValue> mDictionary;
        // Methods & Properties for IDictionary implementation would defer to mDictionary:
        public void Add(TKey key, TValue value){
            mDictionary.Add(key, value);
            OnCollectionChanged(NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, value)
            return;
        }
        // Implementation of INotifyCollectionChanged:
        public event NotifyCollectionChangedEventHandler CollectionChanged;
        protected void OnCollectionChanged(NotifyCollectionChangedEventArgs args){
            // event fire implementation
        }
        // Implementation of INotifyProperyChanged:
        public event ProperyChangedEventHandler ProperyChanged;
        protected void OnPropertyChanged(PropertyChangedEventArgs args){
            // event fire implementation
        }
    }
    

    编辑:

    ICollection<KeyValuePair<TKey,TValue>>
    IEnumerable<KeyValuePair<TKey,TValue>>
    IEnumerable.
    

    根据您的需要,您可能不必实现整个IDictionary接口,如果您只需要调用几个方法,那么只需实现这些方法,IDictionary接口就成了一种奢侈品。必须实现INotifyCollectionChanged和INotifyPropertyChanged接口,绑定才能工作但是,大引号

        4
  •  1
  •   eka808    10 年前

    比如说:

    ObservableCollection<Tuple<string, object>>()
    

    当然,字符串、对象和示例类型

        5
  •  1
  •   hirse herrlock    6 年前
    public class ObservableDictonary<TKey, TValue> : Dictionary<TKey, TValue>, INotifyCollectionChanged, INotifyPropertyChanged
    {
        public event NotifyCollectionChangedEventHandler CollectionChanged;
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public new void Add(TKey key, TValue value)
        {
            base.Add(key, value);
            if (!TryGetValue(key, out _)) return;
            var index = Keys.Count;
            OnPropertyChanged(nameof(Count));
            OnPropertyChanged(nameof(Values));
            OnCollectionChanged(NotifyCollectionChangedAction.Add, value, index);
        }
    
        public new void Remove(TKey key)
        {
            if (!TryGetValue(key, out var value)) return;
            var index = IndexOf(Keys, key);
            OnPropertyChanged(nameof(Count));
            OnPropertyChanged(nameof(Values));
            OnCollectionChanged(NotifyCollectionChangedAction.Remove, value, index);
            base.Remove(key);
        }
    
        public new void Clear()
        {
    
        }
    
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChanged?.Invoke(this, e);
        }
    
        protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            CollectionChanged?.Invoke(this, e);
        }
    
        private void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    
        private void OnCollectionChanged(NotifyCollectionChangedAction action, object item)
        {
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item));
        }
    
        private void OnCollectionChanged(NotifyCollectionChangedAction action, object item, int index)
        {
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item, index));
        }
    
        private int IndexOf(KeyCollection keys, TKey key)
        {
            var index = 0;
            foreach (var k in keys)
            {
                if (Equals(k, key))
                    return index;
                index++;
            }
            return -1;
        }
    }