您的位置:首页 > 运维架构

PropertyGird 控件的简单用法

2010-12-15 18:00 357 查看
PropertyGird 控件 , 我想大多数朋友都不陌生 , 因为 Visual Studio 里对属性的操作就是通过 PropertyGird 控件来实现的.

PropertyGird - 常用属性 :

1. SelectedObject
通俗的讲 SelectedObject 属性就是用来指定对象的 ;
如果是在对象列表中则是指定的列表中的第一个对象 ;
如果当前没有选定任何对象 , 返回值则为 " 空引用 " (在 Visual Basic 中为 Nothing).
使用语法 :
比如我有一个 Car 类.
PropertyGrid.SelectedObject = new Car;

2. 属性的分组
CategoryAttribute 类.

   指定当属性 (Property) 或事件显示在一个设置为 " 按分类顺序 " 模式的 PropertyGrid 控件中时 , 用于给属性或事件分组的类别的名称.

   [CategoryAttribute("分组的名称")] - 使用这样的格式在属性的上方来进行属性的分组.

   使用方法 :

    public class Car
{
private Color black;

[CategoryAttribute("外观")]
private Color Black
{
get { return this.black; }
set { this.black = value; }
}
}


3. 属性的值状态

ReadOnlyAttribute 类.

指定该属性 (Attribute) 所绑定到的属性 (Property) 在设计时是只读属性 (Property) 还是读/写属性 (Property) .无法继承此类.

[ReadOnlyAttribute (true)] - 使用这样的格式在属性的上方来进行属性的只读进行控制.( 值 [True & False] )

使用方法 :

    public class Car
{
private Color black;

[ReadOnlyAttribute(true)]
private Color Black
{
get { return this.black; }
set { this.black = value; }
}
}


4. 设置属性的帮助说明

Description .

指定当选中该属性时控件下方的帮助说明文字.

[Description("颜色的设置")] - 使用这样的格式在属性的上方来进行属性的说明的输出.

使用方法 :

    public class Car
{
private Color black;

[Description("颜色的设置")]
private Color Black
{
get { return this.black; }
set { this.black = value; }
}
}


设置的多项时 , 使用逗号隔开.

    public class Car
{
private Color black;

[CategoryAttribute("外观"),
ReadOnlyAttribute(true),
Description("颜色的设置")]
private Color Black
{
get { return this.black; }
set { this.black = value; }
}
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: