PropertyGrid 用法(汉化属性)
来源:互联网 发布:单片机工资怎么样 编辑:程序博客网 时间:2024/05/24 22:44
有的朋友,肯定用到了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 BasePropertyDescriptor (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 一下呀。有什么问题,请大家指证呀。
- PropertyGrid 用法(汉化属性)
- PropertyGrid属性值的汉化
- PropertyGrid用法
- C# 属性页 PropertyGrid
- C#自定义PropertyGrid属性
- C#自定义PropertyGrid属性
- C#自定义PropertyGrid属性
- C#自定义PropertyGrid属性
- C#自定义PropertyGrid属性
- C#自定义PropertyGrid属性
- C#自定义PropertyGrid属性
- C#自定义PropertyGrid属性
- PropertyGrid用法总结
- PropertyGrid一用法心得
- ext中PropertyGrid用法
- winform propertygrid用法
- 动态可订制属性的 PropertyGrid
- PropertyGrid 中 类属性定义
- 在Eclipse中使用CVS
- DOS下NFO文件的查看方法!
- NFO文件名常识
- JAVA讲义1
- 转帖 程序员的TOP25句话
- PropertyGrid 用法(汉化属性)
- 深圳是个旧社会!
- 立即释放.net下的com组件
- 《Java编程思想》(第二版)第08章:接口与内隐类
- 电脑围棋领域的研究概述
- [修改]js图形报表
- 【源译】Optimizeit Profiler概览
- adsl 掉线(转)
- 批注与看书