效果:
代码:
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;
}