NET框架PropertyGrid控件是 Visual Studio .NET 属性浏览器的核心。PropertyGrid控件显示对象或类型的属性,并主要通过使用反射来检索项目的属性

首先创建 PropertyGrid 控件要使用 Visual Studio .NET 创建 PropertyGrid 控件,需要将该控件添加到工具箱中,因为默认情况下并不包含该控件。在 Tools(工具)菜单中,选择 Customize Toolbox(自定义工具箱)。在对话框中选择 Framework Components(框架组件)选项卡,然后选择 PropertyGrid。

PropertyGrid 包含以下部分:

·属性

·可展开属性

·属性类别标题

·属性说明

·属性编辑器

·属性选项卡

·命令窗格(显示控件设计器提供的设计器操作)

PropertyGrid的外观特征

·PropertyGrid 的许多外观特征都可以自定义。下面列出了其中的一部分:

·通过 HelPBackColor、HelpForeColor和 HelpVisible属性可以更改背景颜色、更改字体颜色或隐藏说明窗

·通过 ToolbarVisible属性可以隐藏工具栏,通过 BackColor属性可以更改工具栏的颜色,通过 LargeButtons属性可以显示大工具栏按钮。

·使用 PropertySort属性可以按字母顺序对属性进行排序和分类。

·通过 BackColor属性可以更改拆分器的颜色。

·通过 LineColor属性可以更改网格线和边框。

·本示例中的选项窗口不需要工具栏,因此可以将ToolbarVisible设置为 false。其余属性均保留默认设置。

更改属性的显示方式

  要更改某些属性的显示方式,您可以对这些属性应用不同的特性。特性是用于为类型、字段、方法和属性等编程元素添加批注的声明标记,在运行时可以使用反射对其进行检索。下面列出了其中的一部分:

DescriptionAttribute - 设置显示在属性下方说明帮助窗格中的属性文本。这是一种为活动属性(即具有焦点的属性)提供帮助文本的有效方法。可以将此特性应用于 MaxRepeatRate 属性。

CategoryAttribute - 设置属性在网格中所属的类别。当您需要将属性按类别名称分组时,此特性非常有用。如果没有为属性指定类别,该属性将被分配给杂项类别。可以将此特性应用于所有属性。

BrowsableAttribute – 表示是否在网格中显示属性。此特性可用于在网格中隐藏属性。默认情况下,公共属性始终显示在网格中。可以将此特性应用于 SettingsChanged 属性。

ReadOnlyAttribute – 表示属性是否为只读。此特性可用于禁止在网格中编辑属性。默认情况下,带有 get 和 set 访问函数的公共属性在网格中是可以编辑的。可以将此特性应用于 AppVersion 属性。

DefaultValueAttribute – 表示属性的默认值。如果希望为属性提供默认值,然后确定该属性值是否与默认值相同,则可使用此特性。可以将此特性应用于所有属性。

DefaultPropertyAttribute – 表示类的默认属性。在网格中选择某个类时,将首先突出显示该类的默认属性。

PropertyGrid有一個 property 為「SelectedObject」,用來指向所關連的物件。如下所示:

Class newclass=newClass();(例:propertyGrid控件)

propertyGrid1.SelectedObject = newclass;

PropertyGrid主要应用:

1.自定义一个布尔型的属性下拉框:

private bool _bool;

[CategoryAttribute("布尔型"), DescriptionAttribute("布尔型")]

public bool 布尔型

{

get { return _bool; }

set { _bool = value;}

}

2.自定义一个枚举型的属性下拉框:

publicenum s

{

a = 1,

b = 2,

c = 4

}

private s test = s.a;

[CategoryAttribute("枚举型"), DescriptionAttribute("这是一个枚举的下拉框")]

public s 枚举

{

get { return test; }

set { test = value; }

}

3.自定义一个INT数组类型属性对话框:

privateint[] arr;

[CategoryAttribute("数组型"), DescriptionAttribute("这是一个数组的对话框")]

public int[] 数组

{

get { return arr; }

set { arr = value; }

}

4 属性转换器(TypeConverter)

//自定义的StringConverter类型设置(包括自定义类型转换器

privatestring _fileName="1";

[CategoryAttribute("自定义的复杂类型设置(包括自定义类型转换器)"),TypeConverterAttribute(typeof(FileNameConverter)), ReadOnlyAttribute(false)]

publicstring FileName

{

get{ return this._fileName;}

set{ this._fileName = value;}

}

//定义一个FileNameConverter继承 StringConverter转换器

publicclass FileNameConverter: System.ComponentModel.StringConverter

{

///<summary>

///根据返回值确定是否支持下拉框的形式

///</summary>

///<returns>

/// true: 下来框的形式

/// false: 普通文本编辑的形式

///</returns>

publicoverride boolGetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContextcontext)

{

return true;

}

///下拉框中具体的内容

publicoverride System.ComponentModel.TypeConverter.StandardValuesCollectionGetStandardValues(System.ComponentModel.ITypeDescriptorContextcontext)

{

return new StandardValuesCollection(newstring[] { "1","2", "3"});

}

///根据返回值确定是否是不可编辑的文本框

/// true: 文本框不可以编辑

/// flase: 文本框可以编辑

publicoverride boolGetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContextcontext)

{

return true;

}

}

5.自定义一个带图片的属性

privateGrade x = newGrade(100);

publicGrade X

{

get

{

return x;

}

set

{

x = value;

}

}

///指定用作此属性所绑定到的对象的转换器的类型

[TypeConverterAttribute(typeof(ExpandableObjectConverter)),Category("自定义带图片属性"), Description("自定义带图片属性")]

//指定用来更改属性的编辑器

[Editor(typeof(GradeEditor),typeof(UITypeEditor))]

publicstruct Grade

{

privateint grade;

publicGrade(int grade)

{

this.grade = grade;

}

publicint Value

{

get

{

return grade;

}

}

}

publicclass GradeEditor: UITypeEditor

{

// 指示指定的上下文是否支持在指定的上下文内绘制对象值的表示形式。

//

//参数:

// context:

// 可用于获取附加上下文信息的 System.ComponentModel.ITypeDescriptorContext。

//

//返回结果:

// 如果实现System.Drawing.Design.UITypeEditor.PaintValue(System.Object,System.Drawing.Graphics,System.Drawing.Rectangle),则为

// true;否则为 false。

publicoverride boolGetPaintValueSupported(ITypeDescriptorContextcontext)

{

return true;

}

//使用指定的System.Drawing.Design.PaintValueEventArgs 绘制某个对象的值的表示形式。

publicoverride voidPaintValue(PaintValueEventArgs pe)

{

// 选择根据valu的正确的位图

string bmpName = null;

Grade g = (Grade)pe.Value;

if (g.Value > 80)

{ //可修改 bmpName à图片

// bmpName = @"C:\Documents andSettings\Administrator\桌面\ll\ll\2.ico";

}

else if (g.Value >60)

{

//bmpName = @"C:\Documents and Settings\Administrator\桌面\ll\ll\3.ico";

}

else

{

// bmpName = @"C:\Documents andSettings\Administrator\桌面\ll\ll\4.ico";

}

// 画在表面上的位图提供了// 多个图片Bitmap b = new Bitmap(typeof(GradeEditor),bmpName)。

Bitmap b = new Bitmap(global::propertyGrid.Properties.Resources.t1);

//指示绘制内容和绘制位置pe.Bounds-->获取图片是矩形

pe.Graphics.DrawImage(b, pe.Bounds);

//释放图象的对象

b.Dispose();

}

}

如图1所示: 如图2所示:

6属性框选项卡的扩展 -à属性窗口的工具栏上有一个像闪电的按钮,按下这个按钮属性窗口就会切换到属性二

//标识为要显示的属性选项卡 PropertyTabScope.Component:此选项卡被添加到当前的”属性“窗口中去

[PropertyTabAttribute(typeof(TypeCategoryTab),PropertyTabScope.Component)]

classClass

{

private string _属性二 ;

[BrowsableAttribute(false)]

public string属性二

{

get{return _属性二; }

set{ _属性二=value;}

}

public classTypeCategoryTab :System.Windows.Forms.Design.PropertyTab

{

// 提供自定义一个 选项卡按扭 名字

publicoverride stringTabName

{

get

{

return"属性(二)";

}

}

// 提供选项卡按扭 的位图

publicoverride System.Drawing.Bitmap Bitmap

{

get

{

Bitmapbmp = new Bitmap(global::propertyGrid.Properties.Resources.t1);

returnbmp;

}

}

//获取指定组件属性的集合的对象

//component:检索属性的组件

//attributes:检索的属性(Property) 的属性 (Attribute) 的System.Attribute 类型的数组

publicoverride System.ComponentModel.PropertyDescriptorCollection GetProperties(object component, System.Attribute[]attributes)

{///检索 属性框中属性为隐藏的->[BrowsableAttribute(false)]的属性集合

PropertyDescriptorCollectionprops;

Attribute[]att = new Attribute[]{ BrowsableAttribute.No};

if(attributes == null)

props = TypeDescriptor.GetProperties(component);

else

props = TypeDescriptor.GetProperties(component, att);

return props;

}

/// 获取指定组件的属性。

publicoverride System.ComponentModel.PropertyDescriptorCollection GetProperties(object component)

{

return this.GetProperties(component,null);

}

}

}