Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访问自定义子词典<;TKey,TValue>;通过自定义词典<;TKey,TValue>; 问题 如何在BindableDictionary()中实现代码行“object System.Collections.IList.this[int index]{get{throw new NotImplementedException();}set{throw new NotImplementedException();}”,以便编译下面的代码 请继续读下去 脚本 我有一个自定义的BindableDictionary() 我使用它绑定到构成DataGrid数据源的BindingSource 因此,当我更新字典时,它会自动更新网格 问题_C#_Dictionary - Fatal编程技术网

C# 访问自定义子词典<;TKey,TValue>;通过自定义词典<;TKey,TValue>; 问题 如何在BindableDictionary()中实现代码行“object System.Collections.IList.this[int index]{get{throw new NotImplementedException();}set{throw new NotImplementedException();}”,以便编译下面的代码 请继续读下去 脚本 我有一个自定义的BindableDictionary() 我使用它绑定到构成DataGrid数据源的BindingSource 因此,当我更新字典时,它会自动更新网格 问题

C# 访问自定义子词典<;TKey,TValue>;通过自定义词典<;TKey,TValue>; 问题 如何在BindableDictionary()中实现代码行“object System.Collections.IList.this[int index]{get{throw new NotImplementedException();}set{throw new NotImplementedException();}”,以便编译下面的代码 请继续读下去 脚本 我有一个自定义的BindableDictionary() 我使用它绑定到构成DataGrid数据源的BindingSource 因此,当我更新字典时,它会自动更新网格 问题,c#,dictionary,C#,Dictionary,我有一个场景,我希望有一个字典,其中值是一个自定义对象,该自定义对象包含BindableDictionary作为其属性之一 public class MyClass { public string Name { get; set; } public BindableDictionary<string, string> SubDictionary { get; private set; } public MyClass(string name) {

我有一个场景,我希望有一个字典,其中值是一个自定义对象,该自定义对象包含BindableDictionary作为其属性之一

public class MyClass
{
    public string Name { get; set; }
    public BindableDictionary<string, string> SubDictionary { get; private set; }
    public MyClass(string name)
    {
        Name = name;
        SubDictionary = new BindableDictionary<string, string>();
    }
}
它不起作用的原因
  • 我不能这样做,因为我还没有在我的BindableDictionary中实现这个功能,我有点被困在如何做到这一点上
  • 请有人解释一下我该怎么做
  • 非常感谢你的帮助
迄今为止的BindableDictionary代码
公共类BindableDictionary:IDictionary,IBindingList
{
私有字典源=新字典();
void IBindingList.AddIndex(PropertyDescriptor属性){}
对象IBindingList.AddNew(){抛出新的NotImplementedException();}
bool-IBindingList.AllowEdit{get{return false;}}
bool-IBindingList.AllowNew{get{return false;}}
bool IBindingList.AllowRemove{get{return false;}}
void IBindingList.ApplySort(PropertyDescriptor属性,ListSortDirection方向){}
int-IBindingList.Find(PropertyDescriptor属性,对象键){抛出新的NotImplementedException();}
bool-IBindingList.IsSorted{get{return false;}}
void IBindingList.RemoveIndex(PropertyDescriptor属性){}
void IBindingList.RemoveSort(){}
ListSortDirection IBindingList.SortDirection{get{return ListSortDirection.Ascending;}
PropertyDescriptor IBindingList.SortProperty{get{return null;}}
bool-IBindingList.SupportsChangeNotification{get{return true;}}
bool-IBindingList.supportsserching{get{return false;}}
bool-IBindingList.SupportsSorting{get{return false;}}
int System.Collections.IList.Add(对象值){抛出新的NotImplementedException();}
void System.Collections.IList.Clear(){Clear();}
bool System.Collections.IList.Contains(对象值){if(值为TKey){return source.ContainsKey((TKey)值);}else if(值为TValue){return source.ContainsValue((TValue)值);}return false;}
int System.Collections.IList.IndexOf(对象值){return-1;}
void System.Collections.IList.Insert(int索引,对象值){throw new NotImplementedException();}
bool System.Collections.IList.IsFixedSize{get{return false;}}
bool System.Collections.IList.IsReadOnly{get{return true;}}
void System.Collections.IList.Remove(对象值){if(值为TKey){Remove((TKey)值);}
void System.Collections.IList.RemoveAt(int index){throw new NotImplementedException();}
对象System.Collections.IList.this[int index]{get{throw new NotImplementedException();}set{throw new NotImplementedException();}}
私有ListChangedEventHandler listChanged;
事件ListChangedEventHandler IBindingList.ListChanged
{
添加{listChanged+=value;}
删除{listChanged-=value;}
}
受保护的虚拟void OnListChanged(ListChangedEventArgs e)
{
var-evt=listChanged;
如果(evt!=null)evt(this,e);
}
公共无效添加(TKey键,TValue值)
{
source.Add(键、值);
OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1));
}
公用门移除(TKey)
{
如果(源。删除(键))
{
OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1));
返回true;
}
返回false;
}
公共TValue此[TKey]
{
收到
{
返回源[键];
}
设置
{
源[键]=值;
OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1));
}
}
void ICollection.Add(KeyValuePair项)
{
((ICollection)来源)。添加(项);
OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1));
}
布尔ICollection.Remove(KeyValuePair项)
{
如果(((ICollection)源)。删除(项))
{
OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1));
返回true;
}
返回false;
}
public bool ContainsKey(TKey){return source.ContainsKey(key);}
公共ICollection键{get{return source.Keys;}}
public bool TryGetValue(TKey-key,out-TValue值){return source.TryGetValue(key,out-value);}
公共ICollection值{get{return source.Values;}}
public void Clear(){source.Clear();}
bool ICollection.Contains(KeyValuePair项){return((ICollection)source.Contains(项);}
void ICollection.CopyTo(KeyValuePair[]数组,int-arrayIndex){((ICollection)源).CopyTo(数组,arrayIndex);}
公共整数计数{get{return source.Count;}}
bool ICollection.IsReadOnly{get{return((ICollection)source).IsReadOnly;}
公共IEnumerator GetEnumerator(){返回源。GetEnumerator();}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator(){返回GetEnumerator();
private BindingSource _bindingSource = new BindingSource();
private BindableDictionary<string, MyClass> myTopLevelDictionary = new BindableDictionary<string, MyClass>();



dataGridView1.DataSource = _bindingSource;

 myTopLevelDictionary.Add("Jim", new MyClass("Jim"));
            myTopLevelDictionary["Jim"].SubDictionary.Add("Height", "180cm");
            myTopLevelDictionary["Jim"].SubDictionary.Add("Weight", "12Stone");
            myTopLevelDictionary.Add("Bob", new MyClass("Bob"));
            myTopLevelDictionary["Bob"].SubDictionary.Add("Height", "170cm");
            myTopLevelDictionary["Bob"].SubDictionary.Add("Weight", "11Stone");

            _bindingSource.DataSource = myTopLevelDictionary["Jim"].SubDictionary;
_bindingSource.DataSource = myTopLevelDictionary["Jim"].SubDictionary;
public class BindableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IBindingList
    {
        private Dictionary<TKey, TValue> source = new Dictionary<TKey, TValue>();

        void IBindingList.AddIndex(PropertyDescriptor property) { }
        object IBindingList.AddNew() { throw new NotImplementedException(); }
        bool IBindingList.AllowEdit { get { return false; } }
        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 { throw new NotImplementedException(); } 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);

            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); } 
    }
public class BindableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IBindingList
    {
        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 false; } }
        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); }
    }