C#BindingDictionary(of T)实现

时间:2011-08-10 10:29:29

标签: c#

摘要

  • 前段时间我决定使用BindingList连接到数据源并从传入服务接收自动更新(并同时刷新我的数据网格)。
  • 我开始意识到的是,对于我来说(对于轻松访问数据而言)的效率要高得多,而BindingDictionary则具有完全相同的功能。
  • 我最终实现了以下代码,该代码非常适用于更新......
  • ... 但是 ...当BindingDictionary已绑定到我的数据网格时我会失败,我尝试向它添加一个新对象(新对象永远不会出现)。
  • 我知道我错过了部分实施.....

......任何人都可以帮我完成实施吗?

 [Serializable]
[XmlRoot("dictionary")]
[KnownType(typeof(BindableDictionary<string, string>))]
public class BindableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IBindingList, IXmlSerializable
{
    #region Serialize Stuff

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    #region READ XML

    public void ReadXml(System.Xml.XmlReader reader)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

        bool wasEmpty = reader.IsEmptyElement;
        reader.Read();

        if (wasEmpty)
            return;

        while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");
            reader.ReadStartElement("key");
            TKey key = (TKey)keySerializer.Deserialize(reader);
            reader.ReadEndElement();
            reader.ReadStartElement("value");
            TValue value = (TValue)valueSerializer.Deserialize(reader);
            reader.ReadEndElement();
            this.Add(key, value);
            reader.ReadEndElement();
            reader.MoveToContent();
        }
        reader.ReadEndElement();
    }

    #endregion READ XML

    #region WRITE XML

    public void WriteXml(System.Xml.XmlWriter writer)
    {

        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

        foreach (TKey key in this.Keys)
        {
            writer.WriteStartElement("item");
            writer.WriteStartElement("key");
            keySerializer.Serialize(writer, key);
            writer.WriteEndElement();
            writer.WriteStartElement("value");
            TValue value = this[key];
            valueSerializer.Serialize(writer, value);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }

    #endregion WRITE XML

    #endregion Serialize Stuff

    private Dictionary<TKey, TValue> source = new Dictionary<TKey, TValue>();
    private Dictionary<int, TKey> sourceKey = new Dictionary<int, TKey>();

    void IBindingList.AddIndex(PropertyDescriptor property) { }
    object IBindingList.AddNew() { throw new NotImplementedException(); }
    bool IBindingList.AllowEdit { get { return true; } }
    bool IBindingList.AllowNew { get { return false; } }
    bool IBindingList.AllowRemove { get { return false; } }
    void IBindingList.ApplySort(PropertyDescriptor property, ListSortDirection direction) { }
    int IBindingList.Find(PropertyDescriptor property, object key) { throw new NotImplementedException(); }
    bool IBindingList.IsSorted { get { return false; } }
    void IBindingList.RemoveIndex(PropertyDescriptor property) { }
    void IBindingList.RemoveSort() { }
    ListSortDirection IBindingList.SortDirection { get { return ListSortDirection.Ascending; } }
    PropertyDescriptor IBindingList.SortProperty { get { return null; } }
    bool IBindingList.SupportsChangeNotification { get { return true; } }
    bool IBindingList.SupportsSearching { get { return false; } }
    bool IBindingList.SupportsSorting { get { return false; } }
    int System.Collections.IList.Add(object value) { throw new NotImplementedException(); }
    void System.Collections.IList.Clear() { Clear(); }
    bool System.Collections.IList.Contains(object value) { if (value is TKey) { return source.ContainsKey((TKey)value); } else if (value is TValue) { return source.ContainsValue((TValue)value); } return false; }
    int System.Collections.IList.IndexOf(object value) { return -1; }
    void System.Collections.IList.Insert(int index, object value) { throw new NotImplementedException(); }
    bool System.Collections.IList.IsFixedSize { get { return false; } }
    bool System.Collections.IList.IsReadOnly { get { return true; } }
    void System.Collections.IList.Remove(object value) { if (value is TKey) { Remove((TKey)value); } }
    void System.Collections.IList.RemoveAt(int index) { throw new NotImplementedException(); }

    object System.Collections.IList.this[int index]
    {
        get
        {
            return source[sourceKey[index]];
        }
        set { throw new NotImplementedException(); }
    }

    private ListChangedEventHandler listChanged;

    event ListChangedEventHandler IBindingList.ListChanged
    {
        add { listChanged += value; }
        remove { listChanged -= value; }
    }

    protected virtual void OnListChanged(ListChangedEventArgs e)
    {
        var evt = listChanged;

        if (evt != null) evt(this, e);
    }

    public void Add(TKey key, TValue value)
    {
        source.Add(key, value);
        sourceKey.Add(sourceKey.Count, key);
        OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
    }

    public bool Remove(TKey key)
    {
        if (source.Remove(key))
        {
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));

            return true;
        }

        return false;
    }

    public TValue this[TKey key]
    {
        get
        {
            return source[key];
        }
        set
        {
            source[key] = value;

            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }
    }

    void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
    {
        ((ICollection<KeyValuePair<TKey, TValue>>)source).Add(item);

        OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
    }

    bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
    {
        if (((ICollection<KeyValuePair<TKey, TValue>>)source).Remove(item))
        {
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));

            return true;
        }

        return false;
    }

    public bool ContainsKey(TKey key) { return source.ContainsKey(key); }
    public ICollection<TKey> Keys { get { return source.Keys; } }
    public bool TryGetValue(TKey key, out TValue value) { return source.TryGetValue(key, out value); }
    public ICollection<TValue> Values { get { return source.Values; } }
    public void Clear() { source.Clear(); }
    bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) { return ((ICollection<KeyValuePair<TKey, TValue>>)source).Contains(item); }
    void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) { ((ICollection<KeyValuePair<TKey, TValue>>)source).CopyTo(array, arrayIndex); }
    public int Count { get { return source.Count; } }
    bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly { get { return ((ICollection<KeyValuePair<TKey, TValue>>)source).IsReadOnly; } }
    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return source.GetEnumerator(); }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }
    bool ICollection.IsSynchronized { get { return false; } }
    object ICollection.SyncRoot { get { return null; } }
    void ICollection.CopyTo(Array array, int arrayIndex) { ((ICollection)source).CopyTo(array, arrayIndex); }
}

0 个答案:

没有答案