Winform PropertyGrid嵌套显示

重要的两点:

1.指定转换器;

2.属性赋值;

 public class School
    {
	    [DisplayName("地址")]
		public string Address{set;get;}
		[DisplayName("姓名")]

		public string Name{set;get;}
		[DisplayName("性别")]

		public string Sex{set;get;}
        public override string  ToString()
        {
            return Address+Name+Sex;
        }
    }
    public class SystemConfig
    {
	    [DisplayName("名称"), Category("地址")] 
	    public string ConfigName { set; get; } = "A";
        private School _School = new School();
        [TypeConverter(typeof(ExpandableObjectConverter)),DisplayName("学校")]
        public School MySchool1
        {
            set { _School = value; }
            get { return _School; }
        }
        public override string ToString()
        {
            return ConfigName + MySchool1;
        }
    }
    public class Attributes
    {
        [Category("学生")]
        public string Ages { get; set; }
        
        private SystemConfig _config = new SystemConfig();
        [TypeConverter(typeof(ExpandableObjectConverter)), Category("其他"),DisplayName("配置")]
        public SystemConfig Config
        {
            set { _config = value; }
            get { return _config; }
        }
    }

1.   重写  ToString() 是为了在标题后面显示,如下图中配置后面的A;

2. 指定转换器说的是 [TypeConverter(typeof(ExpandableObjectConverter)),DisplayName("学校")]中的TypeConverter特性,ExpandableObjectConverter是内置的转换器,可以直接用,或者自定义子类,可参考http://t.csdnimg.cn/EaLO4

3.属性赋值说的是private SystemConfig _config = new SystemConfig();就是直接给字段默认值,要不然嵌套对象里面属性没有值,是不会显示嵌套的,比如private School _School = new School();改为private School _School;

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinForm PropertyGrid 控件是一个可以让用户对对象的属性进行编辑的界面控件。它可以通过绑定数据源的方式自动显示对象的不同属性,并且提供了很多自定义的设置。 在WinForm PropertyGrid 控件中,下拉项是其中一个常用的自定义选项。下拉项可以让用户在属性的编辑界面中选择一个值,而不需要手动输入。 要在PropertyGrid 中实现下拉项,可以通过使用 System.ComponentModel 属性类库中的类和特性来实现。首先,我们可以通过定义一个枚举类型来代表下拉项中的所有选项,然后通过在属性上应用 System.ComponentModel 类中的属性特性,将该属性设置为下拉列表。比如我们可以使用 System.ComponentModel.Category 属性特性来设置属性的类别,使用 System.ComponentModel.Description 属性特性来为属性提供描述,使用 System.ComponentModel.TypeConverter 属性特性来指定属性类型的转换器等。 在设置完属性的特性后,我们可以将该属性绑定到 PropertyGrid 控件中,设置 PropertyGrid.SelectedObject 属性为包含该属性的对象,然后将 PropertyGrid 控件加入到窗体中。这样,当用户点击 PropertyGrid 控件中的选项时,会显示下拉列表供用户选择。 总的来说,通过在属性上设置 System.ComponentModel 属性特性,并将该属性绑定到 PropertyGrid 控件中,我们可以实现 WinForm PropertyGrid 控件中的下拉项功能,方便用户进行属性的选择和编辑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值