有的朋友,肯定用到了propertygrid中要将一个对像的属性给汉化过来,以合适咱们中国人的习惯.
其实这个汉化,ms是支持的:请瞧下面的代码,你只需要copy,然后做为你的对像的父类,然后在你的对像的属性上加入中文意思,就搞定了。一切都那么简单:
#region 所有要放在propertygird中的对像的基类.
public class baseobject : icustomtypedescriptor
{
private propertydescriptorcollection globalizedprops;
public string getclassname()
{
return typedescriptor.getclassname(this,true);
}
public attributecollection getattributes()
{
return typedescriptor.getattributes(this,true);
}
public string getcomponentname()
{
return typedescriptor.getcomponentname(this, true);
}
public typeconverter getconverter()
{
return typedescriptor.getconverter(this, true);
}
public eventdescriptor getdefaultevent()
{
return typedescriptor.getdefaultevent(this, true);
}
public propertydescriptor getdefaultproperty()
{
return typedescriptor.getdefaultproperty(this, true);
}
public object geteditor(type editorbasetype)
{
return typedescriptor.geteditor(this, editorbasetype, true);
}
public eventdescriptorcollection getevents(attribute[] attributes)
{
return typedescriptor.getevents(this, attributes, true);
}
public eventdescriptorcollection getevents()
{
return typedescriptor.getevents(this, true);
}
public propertydescriptorcollection getproperties(attribute[] attributes)
{
if ( globalizedprops == null)
{
propertydescriptorcollection baseprops = typedescriptor.getproperties(this, attributes, true);
globalizedprops = new propertydescriptorcollection(null);
foreach( propertydescriptor oprop in baseprops )
{
globalizedprops.add(new globalizedpropertydescriptor(oprop));
}
}
return globalizedprops;
}
public propertydescriptorcollection getproperties()
{
if ( globalizedprops == null)
{
propertydescriptorcollection baseprops = typedescriptor.getproperties(this, true);
globalizedprops = new propertydescriptorcollection(null);
foreach( propertydescriptor oprop in baseprops )
{
globalizedprops.add(new basepropertydescriptor(oprop));
}
}
return globalizedprops;
}
public object getpropertyowner(propertydescriptor pd)
{
return this;
}
}
#endregion
#region 所以要放在propertygird中的对像的描绘进行重写
public class basepropertydescriptor : propertydescriptor
{
private propertydescriptor basepropertydescriptor;
public basepropertydescriptor(propertydescriptor basepropertydescriptor) : base(basepropertydescriptor)
{
this.basepropertydescriptor = basepropertydescriptor;
}
public override bool canresetvalue(object component)
{
return basepropertydescriptor.canresetvalue(component);
}
public override type componenttype
{
get { return basepropertydescriptor.componenttype; }
}
public override string displayname
{
get
{
string svalue = "";
foreach(attribute attribute in this.basepropertydescriptor.attributes)
{
if (attribute is showchinese)
{
svalue = attribute.tostring();
break;
}
}
if (svalue == "") return this.basepropertydescriptor.name;
else return svalue;
}
}
public override string description
{
get
{
return this.basepropertydescriptor.description;
}
}
public override object getvalue(object component)
{
return this.basepropertydescriptor.getvalue(component);
}