有的朋友,肯定用到了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); } public override bool IsReadOnly { get { return this.basePropertyDescriptor.IsReadOnly; } } public override string Name { get { return this.basePropertyDescriptor.Name; } } public override Type PropertyType { get { return this.basePropertyDescriptor.PropertyType; } } public override void ResetValue(object component) { this.basePropertyDescriptor.ResetValue(component); } public override bool ShouldSerializeValue(object component) { return this.basePropertyDescriptor.ShouldSerializeValue(component); } public override void SetValue(object component, object value) { this.basePropertyDescriptor.SetValue(component, value); } } #endregion
#region 自定义属性用来显示左的边的汉字 [AttributeUsage(AttributeTargets.Property)] public class showChinese : System.Attribute { private string sChineseChar = ""; public showChinese(string sChineseChar) { this.sChineseChar = sChineseChar; } public string ChineseChar { get { return this.sChineseChar; } } public override string ToString() { return this.sChineseChar; } } #endregion 用法如下: public class testA : BaseObject { [ CategoryAttribute("全局设置"), ReadOnlyAttribute(false), DescriptionAttribute("流程的显示中文名称."), showChinese("流程名称:") //在这儿用这个就行了。哈哈。 ] public string FlowName { get { return strFlowName; } set { strFlowName = value; } } } 快点copy ,try 一下呀。有什么问题,请大家指证呀。 
|