C# PropertyGrid 演练一

来源:互联网 发布:ubuntu elk 搭建 编辑:程序博客网 时间:2024/05/14 13:13

今天因为学习需要第一次用了.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;        }

搞定了!

今天就先认识一下这点好了,后面再把一些收获补充。


原创粉丝点击