System.Windows.Forms.PropertyGrid 属性控件

效果:


代码:

        class Go
        {
            private Color _fillcolor = Color.White;
            private DashStyle _linestyle = DashStyle.Solid;
            [Category("效果"),Description("说明:填充颜色")]
            public Color 填充颜色
            {
                get{ return this._fillcolor;}
                set{this._fillcolor = value;}
            }
            [Category("效果"), Description("线的样式")]
            public DashStyle 线的样式
            {
                get{return this._linestyle;}
                set{this._linestyle = value;}
            }
        }

绑定

propertyGrid1.SelectedObject = new Go();



另一个实例:

今天因为学习需要第一次用了.NET中的PropertyGrid控件,感觉这个控件的功能挺不错,很适合浏览或者设置一个特定对象的属性。这有点类似于VS中的Property View,太有用了。下面就具体说一下怎么用吧。

首先写一个和PropertyGrid绑定的类,设置一些属性,这些属性就是待会要在PropertyGrid中显示的内容。为了简单起见,我就写一个一般的类吧

using System;
using System.ComponentModel;

namespace Testing
{
    [DefaultPropertyAttribute("Name")]
    public class Customer
    {
        private string name;
        private string email;
        private string mark;

        [CategoryAttribute("用户信息"), DescriptionAttribute("设置姓名")]
        public string Name 
        { 
            get { return name; } 
            set { name = value; } 
        }

        [CategoryAttribute("用户信息")]
        public string Email 
        { 
            get{return email;}
            set{email=value;}
        }

        [CategoryAttribute("用户信息")]
        public string Mark
        {
            get { return mark; }
            set { mark = value; }
        }

        public Customer() { }
    }
}

注意引用System.ComponentModel命名空间,将使用到该命名空间中的一些Attribute类,在上面的例子中主要使用了DefaultPropertyAttribute,CategoryAttribute和DescriptionAttribute三个Attribute,至于这三个Attribute有什么用可以通过运行程序来观察。

接下来要做的事就简单了,新建一个windows form application,然后在窗体中拉入一个PropertyGrid控件,在窗体的Load事件中给PropertyGrid控件绑定内容:

private void Form1_Load(object sender, EventArgs e)
        {
            cus = new Customer();
            cus.Name = "小明";
            cus.Email = "xx@yy.com";
            this.propertyGrid1.SelectedObject = cus;
        }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tiz198183

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值