关于如何更有效的定制 PropertyGrid 的显示,有很多相关的文章,例如这个 http://www.cnblogs.com/mywebname/archive/2007/11/15/959732.htmlhttp://msdn.microsoft.com/zh-cn/magazine/cc163804(en-us).aspx等等。google 一下还有更多的内容。这里只是简单的就自己的体会说说。
关于如何更有效的定制 PropertyGrid 的显示,有很多相关的文章,例如这个 http://www.cnblogs.com/mywebname/archive/2007/11/15/959732.htmlhttp://msdn.microsoft.com/zh-cn/magazine/cc163804(en-us).aspx等等。google 一下还有更多的内容。这里只是简单的就自己的体会说说。
实现 ICustomTypeDescriptor 的方法目前应该是所见过的方法中最简单的,对象只需要实现 ICustomTypeDescriptor 接口,并且接口的实现代码基本是委托给 TypeDescriptor 对象的,只需要根据需要重写几个相关的方法即可。例如属性排序:
![]()
Code
1![]()
ICustomTypeDescriptor 成员#region ICustomTypeDescriptor 成员
2![]()
3
AttributeCollection ICustomTypeDescriptor.GetAttributes()
4![]()
{
5
return TypeDescriptor.GetAttributes(this, true);
6
}
7![]()
8
string ICustomTypeDescriptor.GetClassName()
9![]()
{
10
return TypeDescriptor.GetClassName(this, true);
11
}
12![]()
13
string ICustomTypeDescriptor.GetComponentName()
14![]()
{
15
return TypeDescriptor.GetComponentName(this, true);
16
}
17![]()
18
TypeConverter ICustomTypeDescriptor.GetConverter()
19![]()
{
20
return TypeDescriptor.GetConverter(this, true);
21
}
22![]()
23
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
24![]()
{
25
return TypeDescriptor.GetDefaultEvent(this, true);
26
}
27![]()
28
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
29![]()
{
30
return TypeDescriptor.GetDefaultProperty(this, true);
31
}
32![]()
33
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
34![]()
{
35
return TypeDescriptor.GetEditor(this, editorBaseType, true);
36
}
37![]()
38
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
39![]()
{
40
return TypeDescriptor.GetEvents(this, attributes, true);
41
}
42![]()
43
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
44![]()
{
45
return TypeDescriptor.GetEvents(this, true);
46
}
47![]()
48
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
49![]()
{
50
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this, attributes, true);
51![]()
52
return reorderProperties(properties);
53
}
54![]()
55
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
56![]()
{
57
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this, true);
58![]()
59
return reorderProperties(properties);
60
}
61![]()
62
private static PropertyDescriptorCollection reorderProperties(PropertyDescriptorCollection properties)
63![]()
{
64
var list = new PropertyDescriptor[properties.Count];
65![]()
string[] topProps =
{"UUID", "Code", "Name"};
66![]()
67
int i = 0;
68
foreach (string s in topProps)
69![]()
{
70
PropertyDescriptor item = properties.Find(s, true);
71
if (item != null)
72![]()
{
73
list[i] = item;
74
i++;
75
}
76
}
77![]()
78
for (int j = 0; j < properties.Count; j++)
79![]()
{
80
var item = properties[j];
81
if (list.Contains(item))
82![]()
{
83
continue;
84
}
85
list[i] = item;
86
i++;
87
}
88![]()
89
return new PropertyDescriptorCollection(list, true);
90
}
91![]()
92![]()
93
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
94![]()
{
95
return this;
96
}
97![]()
98
#endregion
99![]()
这段代码几乎可以不经修改的拷贝到任何实现ICustomTypeDescriptor 的类中,当然,如果没有超类的话,从 CumtomTypeDescriptor 继承应该也不错,不需要拷贝了,不过为了继承而继承,并不是一个好的习惯,我宁可拷贝一下。