代码之家  ›  专栏  ›  技术社区  ›  Robert Massa

在属性网格中使用字典

  •  12
  • Robert Massa  · 技术社区  · 15 年前

    我想使用PropertyGrid编辑键值(字符串、字符串)项的列表。当我使用 Dictionary<string,string> 作为类型,PropertyGrid将显示一个GUI,但它似乎没有“启用”,即,我不能添加任何项。

    是否支持Dictionary对象,或者是否有其他对象可以解决此问题?

    2 回复  |  直到 9 年前
        1
  •  19
  •   josliber Martin Ballet    9 年前

    我已经做到了 this code 过去:

    class DictionaryPropertyGridAdapter : ICustomTypeDescriptor
    {
        IDictionary _dictionary;
    
        public DictionaryPropertyGridAdapter(IDictionary d)
        {
            _dictionary = d;
        }
    
        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }
    
        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }
    
        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }
    
        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }
    
        EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }
    
        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }
    
        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return _dictionary;
        }
    
        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }
    
        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }
    
        public PropertyDescriptor GetDefaultProperty()
        {
            return null;
        }
    
        PropertyDescriptorCollection
            System.ComponentModel.ICustomTypeDescriptor.GetProperties()
        {
            return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
        }
    
        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            ArrayList properties = new ArrayList();
            foreach (DictionaryEntry e in _dictionary)
            {
                properties.Add(new DictionaryPropertyDescriptor(_dictionary, e.Key));
            }
    
            PropertyDescriptor[] props =
                (PropertyDescriptor[])properties.ToArray(typeof(PropertyDescriptor));
    
            return new PropertyDescriptorCollection(props);
        }
    }
    
    class DictionaryPropertyDescriptor : PropertyDescriptor
    {
        IDictionary _dictionary;
        object _key;
    
        internal DictionaryPropertyDescriptor(IDictionary d, object key)
            : base(key.ToString(), null)
        {
            _dictionary = d;
            _key = key;
        }
    
        public override Type PropertyType
        {
            get { return _dictionary[_key].GetType(); }
        }
    
        public override void SetValue(object component, object value)
        {
            _dictionary[_key] = value;
        }
    
        public override object GetValue(object component)
        {
            return _dictionary[_key];
        }
    
        public override bool IsReadOnly
        {
            get { return false; }
        }
    
        public override Type ComponentType
        {
            get { return null; }
        }
    
        public override bool CanResetValue(object component)
        {
            return false;
        }
    
        public override void ResetValue(object component)
        {
        }
    
        public override bool ShouldSerializeValue(object component)
        {
            return false;
        }
    }
    
    private void Form1_Load(object sender, System.EventArgs e)
    {
        IDictionary d = new Hashtable();
        d["Hello"] = "World";
        d["Meaning"] = 42;
        d["Shade"] = Color.ForestGreen;
    
        propertyGrid1.SelectedObject = new DictionaryPropertyGridAdapter(d);
    }
    

    为我们工作得很好。

        2
  •  2
  •   PolyTekPatrick Leandro Conca    12 年前

    如果您试图将包含dictionary属性的类绑定到propertygrid,而不是直接绑定dictionary本身,那么这里有一个免费的(LGPL许可的)组件可以做到这一点: http://gendictedit.codeplex.com/