PropertyGrid控件动态生成属性及下拉菜单 (转载)

转自:http://blog.sina.com.cn/s/blog_6f14b7010101b91b.html

对于这几个月的这个项目,一直想做个总结,但是鉴于本人记性之差,总是将这件事想起又忘记,终于在这个月工作的最后几天有了几天的空闲,把这个经验好好的记录下来。

 

PropertyGrid,.net框架下的一个控件,这是一个软件升级的项目,原来的软件用的是C++,控件用的还是第三方,这次升级到visual studio .net4.0版本,原以为.net的东西用起来不会费劲的,没想到想要实现项目需要的效果还真没那么简单。

 

由于需要,我这里主要是为了能动态的生成属性页,还要带能动态生成下来菜单,所以今天主要从这方面总结。

 

首先定义一个属性类:

//单条属性类

public class XProp
{
      private string theId = ""; //属性Id,我的项目中需要,大家可以忽略

      private string theCategory = ""; //属性所属类别

      private string theName = "";     //属性名称

      private bool theReadOnly = false;  //属性的只读性,true为只读

      private string theDescription = ""; //属性的描述内容

      private object theValue = null;    //值

      private System.Type theType = null; //类型

      private bool theBrowsable = true;  //显示或隐藏,true为显示

      TypeConverter theConverter = null;  //类型转换

      public string Id
      {
          get { return theId; }

          set { theId = value; }
      }

      public string Category
      {
          get { return theCategory; }

          set { theCategory = value; }
      }
      public bool ReadOnly
      {
          get { return theReadOnly; }

          set { theReadOnly = value; }
      }
      public string Name
      {
          get { return this.theName; }

          set { this.theName = value; }
      }
      public object Value
      {
          get { return this.theValue; }

          set { this.theValue = value; }
      }
      public string Description
      {
          get { return theDescription; }

          set { theDescription = value; }
      }

      public System.Type ProType
      {
          get { return theType; }

          set { theType = value; }
      }

      public bool Browsable
      {
          get { return theBrowsable; }
          set { theBrowsable = value; }
      }

      public virtual TypeConverter Converter
      {
          get { return theConverter; }
          set { theConverter = value; }
      }
}

 

我举一个例子:
private string strdemo;

[DescriptionAttribute("用于举例说明"), 
CategoryAttribute("公有属性"),
DefaultValueAttribute(“测试属性”),
ReadOnlyAttribute(false),
BrowsableAttribute(true), 
TypeConverter(typeof(MyComboTypeConvert))

public string strDemo 
 { 
         get { return strdemo; } 
         set { strdemo = value; } 
}

 

这是个写死的属性,那在我的项目中,根据对象的不同,会需要生产不同的属性页,所以需要一个可以动态生成的属性页,将上述这个一般属性定义,利用XProp类,写成:
Private XProp newXpro = new XProp();
newXpro.Category = ”公有属性”;
newXpro.Name = ” strDemo”;
newXpro.Id = "A";
newXpro.Description = “用于举例说明”;
newXpro.ReadOnly =false;
newXpro.Value = “测试属性”;
newXpro.ProType = typeof(string);
newXpro.Browsable = true;
newXpro.Converter = null;

这样,一条属性就完成了。当然你也可以根据需要自己重写更多的属性相关定义。这里的Converter属性是在后面的下拉菜单中需要用到的,如果不是基础类型的(string,int,bool,enum等),我们可以赋

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值