C#中使用Property Grid 控件 学习笔记(一)
2004-06-30 09:57
1221 查看
1.将Property Grid 控件添加到工具箱中
由于默认情况下Property Grid 控件没有显示在工具箱中所以需要手动添加。
图1 将Property Grid添加到工具箱(在工具箱上右键选择添加/移除项)
图2 在自定义工具箱对话框中选中PropertyGrid控件
2.编写自定义类,并跟PropertyGrid控件进行绑定
using System;
using System.ComponentModel;
namespace PropertyGridDemo
{
[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("用户信息"), DescriptionAttribute("设置消费者Email地址")]
public string Email
{
get
{
return email;
}
set
{
email = value;
}
}
[CategoryAttribute("备注"), DescriptionAttribute("备注信息")]
public string Mark
{
get
{
return mark;
}
set
{
mark = value;
}
}
public Customer()
{
}
}
}
首先定义自定义类型的时候要引用System.ComponentModel命名空间,将使用到该命名空间中的一些Attribute类,在上面的例子中主要使用了DefaultPropertyAttribute,CategoryAttribute和DescriptionAttribute三个Attribute。
CategoryAttribute 指定当属性或事件显示在被一个设置为按分类顺序模式的 System.Windows.Forms.PropertyGrid 控件中时,用于给属性或事件分组的类别的名称。
在编写自定类的时候所有的属性都应该有get 和set方法,如果没有get方法那么这个属性在PropertyGrid中不显示,没有set方法的话则为只读属性在PropertyGrid中无法设置该属性的值。
将自定义类与PropertyGrid控件进行绑定
使用PropertyGrid类的SelectedObject属性进行绑定
private void Form1_Load(object sender, System.EventArgs e)
{
Customer customer = new Customer();
customer.Name = "张三";
customer.Email = "zhangsan@sina.com";
propertyGrid1.SelectedObject = customer;
}
运行结果:
由于默认情况下Property Grid 控件没有显示在工具箱中所以需要手动添加。
图1 将Property Grid添加到工具箱(在工具箱上右键选择添加/移除项)
图2 在自定义工具箱对话框中选中PropertyGrid控件
2.编写自定义类,并跟PropertyGrid控件进行绑定
using System;
using System.ComponentModel;
namespace PropertyGridDemo
{
[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("用户信息"), DescriptionAttribute("设置消费者Email地址")]
public string Email
{
get
{
return email;
}
set
{
email = value;
}
}
[CategoryAttribute("备注"), DescriptionAttribute("备注信息")]
public string Mark
{
get
{
return mark;
}
set
{
mark = value;
}
}
public Customer()
{
}
}
}
首先定义自定义类型的时候要引用System.ComponentModel命名空间,将使用到该命名空间中的一些Attribute类,在上面的例子中主要使用了DefaultPropertyAttribute,CategoryAttribute和DescriptionAttribute三个Attribute。
DefaultPropertyAttribute | 指定组件的默认属性。 |
DescriptionAttribute | 指定属性或事件的说明。 |
将自定义类与PropertyGrid控件进行绑定
使用PropertyGrid类的SelectedObject属性进行绑定
private void Form1_Load(object sender, System.EventArgs e)
{
Customer customer = new Customer();
customer.Name = "张三";
customer.Email = "zhangsan@sina.com";
propertyGrid1.SelectedObject = customer;
}
运行结果:
相关文章推荐
- C#中使用Property Grid 控件 学习笔记
- C#中使用Property Grid 控件 学习笔记(一)
- C#学习笔记__容器控件的使用
- C#学习笔记:控件BackColor属性与ForeColor的使用方法
- c#学习笔记之使用 TableLayoutPanel 控件设置窗体布局
- C#学习笔记-WPF前端布局和控件的使用
- C#学习笔记(20140911)-下拉框、日历、pannel控件的使用
- ASP.NET学习笔记四 控件使用
- C#学习笔记(十六):使用打开保存文件对话框
- C#学习笔记(二十二):使用文件高级
- SilverLight学习笔记--Silverlight中GridSplitter控件的使用
- 汇编语言学习笔记-使用窗体控件
- 『框架设计(第2版)CLR Via C#』学习笔记——使用is和as操作符来进行强制类型转换
- vs2010 学习Silverlight学习笔记(8):使用用户控件
- c#学习笔记三 如何访问另一个页面的控件数据
- WinCE5.0移动平台开发笔记(c#中使用多线程访问winform中控件的若干问题(zt))
- C#学习笔记:使用文件基础
- c# override,new关键字区别与使用(学习笔记)
- Chap 4 学习笔记-使用C#存储变量数据
- C#学习笔记(二十一):使用文件基础