简述
{
List<CustomPropertyDescriptor> tmpPDCLst = new List<CustomPropertyDescriptor>();
PropertyDescriptorCollection tmpPDC = TypeDescriptor.GetProperties(mCurrentSelectObject, attributes);
IEnumerator tmpIe = tmpPDC.GetEnumerator();
CustomPropertyDescriptor tmpCPD;
PropertyDescriptor tmpPD;
while (tmpIe.MoveNext())
{
tmpPD = tmpIe.Current as PropertyDescriptor;
if (mObjectAttribs.ContainsKey(tmpPD.Name))
{
tmpCPD = new CustomPropertyDescriptor(mCurrentSelectObject, tmpPD);
tmpCPD.SetDisplayName(mObjectAttribs[tmpPD.Name]);
//此处用于处理属性分类的名称,可以在XML等设置文件中进行设置,在这段代码中只是简单的在分类后加了“中文”两个字
tmpCPD.SetCategory(tmpPD.Category + "中文");
tmpPDCLst.Add(tmpCPD);
}
}
return new PropertyDescriptorCollection(tmpPDCLst.ToArray());
}
当然在进行属性过虑之后,PropertyGrid中所显示的属性名称都还是原有名称,若想同时改变在PropertyGrid中显示出来的名称则需要重写PropertyDescriptor中的部分方法,在上面这段代码中的CustomPropertyDescriptor就是一个基于PropertyDescriptor的类。
private string mCategory;
public override string Category
{
get { return mCategory; }
}
private string mDisplayName ;
public override string DisplayName
{
get { return mDisplayName; }
}
public void SetDisplayName(string pDispalyName)
{
mDisplayName = pDispalyName;
}
public void SetCategory(string pCategory)
{
mCategory = pCategory;
}
就这样的几步,便可以将PropertyGrid中显示的内容完全自定义。
{
return mCurrentSelectObject;
}
当然也可以通过这个方法来自定义一些Events的输出,
使用方法
XmlNode tmpXNode = mXDoc.SelectSingleNode("Components/Component[@Name=\"Button\"]");
//选择属性设置
XmlNodeList tmpXPropLst = tmpXNode.SelectNodes("Propertys/Property");
//创建CustomProperty对象
CustomProperty cp = new CustomProperty(sender, tmpXPropLst);
//设置PropertyGrid选择对象
propertyGrid1.SelectedObject = cp;
文件下载
http://www.cnblogs.com/pvistely/archive/2006/02/09/327656.html