> Есть колекция обьектовкоторые содержат значения Name и Value, ессественно их количество не известно. Необходимо привязать их к PropertyGrid. Каким образом это сделать?
[TypeConverter(typeof(
MyConverter))]
class MyClass
{
}
public class
MyConverter : TypeConverter
{
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
MyClass mc = value as MyClass;
PropertyDescriptor[] parr = new PropertyDescriptor[mc.Count];
// наполняем parr элементами
MyPropertyDescriptor
PropertyDescriptorCollection ret = new PropertyDescriptorCollection(parr);
return ret;
}
}
public class
MyPropertyDescriptor : PropertyDescriptor
{
// ...
}
данное сообщение получено с www.gotdotnet.ru
ссылка на оригинальное сообщение
Здравствуйте, Аноним, Вы писали:
А>> Есть колекция обьектовкоторые содержат значения Name и Value, ессественно их количество не известно. Необходимо привязать их к PropertyGrid. Каким образом это сделать?
А>
А>
А>[TypeConverter(typeof(MyConverter))]
А>class MyClass
А>{
А>}
А>public class MyConverter : TypeConverter
А>{
А> public override bool GetPropertiesSupported(ITypeDescriptorContext context)
А> {
А> return true;
А> }
А> public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
А> {
А> MyClass mc = value as MyClass;
А> PropertyDescriptor[] parr = new PropertyDescriptor[mc.Count];
А> // наполняем parr элементами MyPropertyDescriptor
А> PropertyDescriptorCollection ret = new PropertyDescriptorCollection(parr);
А> return ret;
А> }
А>}
А>public class MyPropertyDescriptor : PropertyDescriptor
А>{
А> // ...
А>}
А>
данное сообщение получено с www.gotdotnet.ru
А>ссылка на оригинальное сообщение
Только я так и не понял каким образом данные обьектов колекции привязываются к PropertyDescriptor?
> каким образом данные обьектов колекции привязываются к 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
ссылка на оригинальное сообщение