> каким образом данные обьектов колекции привязываются к PropertyDescriptor?
Привязка происходит не к данным (Value), а к Name.
using System.Collections;
class Form1 : Form
{
public Form1()
{
PropertyGrid pg = new PropertyGrid();
pg.Dock = DockStyle.Fill;
pg.Parent = this;
pg.SelectedObject = new Test();
}
}
public class Test
{
public MyHashtable MyHashtable
{
get
{
if (_MyHashtable == null)
_MyHashtable = new MyHashtable();
return _MyHashtable;
}
}
private MyHashtable _MyHashtable;
}
[TypeConverter(typeof(MyHashtableConverter))]
public class MyHashtable : Hashtable
{
public MyHashtable()
{
this["k1"] = "v1";
this["k2"] = "v2";
this["k3"] = "v3";
}
public override string ToString()
{
return "(" + this.Count + ")";
}
}
public class MyHashtableConverter : TypeConverter
{
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
MyHashtable mht = value as MyHashtable;
PropertyDescriptor[] parr = new PropertyDescriptor[mht.Count];
IDictionaryEnumerator de = (IDictionaryEnumerator)mht.GetEnumerator();
int i = 0;
while (de.MoveNext())
{
parr[i++] = new MyPropertyDescriptor(de.Key.ToString());
}
PropertyDescriptorCollection ret = new PropertyDescriptorCollection(parr);
return ret;
}
}
public class MyPropertyDescriptor : PropertyDescriptor
{
public MyPropertyDescriptor(string name) : base(name, null)
{
}
public override bool CanResetValue(object component)
{
return false;
}
public override Type ComponentType
{
get { return typeof(MyHashtable); }
}
public override object GetValue(object component)
{
MyHashtable mht = component as MyHashtable;
return mht
[base.Name];
}
public override bool IsReadOnly
{
get { return false; }
}
public override Type PropertyType
{
get { return typeof(string); }
}
public override void ResetValue(object component)
{
}
public override void SetValue(object component, object value)
{
MyHashtable mht = component as MyHashtable;
mht
[base.Name] = value;
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
}
данное сообщение получено с www.gotdotnet.ru
ссылка на оригинальное сообщение