.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
PropertyGrid 用法(汉化属性)

作者:未知 来源:月光软件站 加入时间:2005-5-13 月光软件站

有的朋友,肯定用到了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 一下呀。有什么问题,请大家指证呀。




相关文章

相关软件