更新:2007 年 11 月
有关属性的概述,请参见 属性概述。定义属性时需考虑以下重要的注意事项:
必须将属性应用于定义的属性。属性用来指定设计器显示属性的方式。有关详细信息,请参见组件的设计时属性 (Attribute)。
如果更改属性会影响到控件的可视显示,请调用 set 访问器中的 Invalidate 方法(此方法是控件从 Control 继承的)。而 Invalidate 又会调用 OnPaint 方法来重绘控件。为提高效率起见,对 Invalidate 的多次调用将产生对 OnPaint 的一次调用。
.NET Framework 类库为常见数据类型(如整数、小数、布尔值和其他数据)提供了类型转换器。类型转换器的目的通常是用来提供字符串到数值的转换(从字符串数据转换为其他数据类型)。常见数据类型与默认类型转换器(将数值转换为字符串,并将字符串转换为相应数据类型)相关联。如果定义了自定义(即,非标准)数据类型的属性,则应用的属性必须将类型转换器指定为与该属性相关联。还可以使用属性使自定义 UI 类型编辑器与某个属性相关联。UI 类型编辑器提供了一个用来编辑属性或数据类型的用户界面。颜色选择器是 UI 类型编辑器的一个示例。在本主题的最后给出了属性的示例。
说明:
如果没有类型转换器或 UI 类型编辑器可供自定义属性使用,则可按 扩展设计时支持 中描述的方法来实现一个。
以下代码片段为自定义控件 FlashTrackBar 定义了一个名为 EndColor 的自定义属性。
Public Class FlashTrackBar
Inherits Control
...
' Private data member that backs the EndColor property.
Private _endColor As Color = Color.LimeGreen
' The Category attribute tells the designer to display
' it in the Flash grouping.
' The Description attribute provides a description of
' the property.
<Category("Flash"), _
Description("The ending color of the bar.")> _
Public Property EndColor() As Color
' The public property EndColor accesses _endColor.
Get
Return _endColor
End Get
Set
_endColor = value
If Not (baseBackground Is Nothing) And showGradient Then
baseBackground.Dispose()
baseBackground = Nothing
End If
' The Invalidate method calls the OnPaint method, which redraws
' the control.
Invalidate()
End Set
End Property
...
End Class
public class FlashTrackBar : Control {
...
// Private data member that backs the EndColor property.
private Color endColor = Color.LimeGreen;
// The Category attribute tells the designer to display
// it in the Flash grouping.
// The Description attribute provides a description of
// the property.
[
Category("Flash"),
Description("The ending color of the bar.")
]
// The public property EndColor accesses endColor.
public Color EndColor {
get {
return endColor;
}
set {
endColor = value;
if (baseBackground != null && showGradient) {
baseBackground.Dispose();
baseBackground = null;
}
// The Invalidate method calls the OnPaint method, which redraws
// the control.
Invalidate();
}
}
...
}
以下代码片段将类型转换器和 UI 类型编辑器与属性 Value 相关联。在这种情况下,Value 是一个整数并具有一个默认的类型转换器,但 TypeConverterAttribute 属性应用一个自定义类型转换器 (FlashTrackBarValueConverter),该类型转换器允许设计器按百分比形式显示 TypeConverterAttribute 属性。UI 类型编辑器 FlashTrackBarValueEditor 允许以可视化的方式显示百分比。此示例还演示了由 TypeConverterAttribute 或 EditorAttribute 属性指定的类型转换器或编辑器如何重写默认的转换器。
<Category("Flash"), _
TypeConverter(GetType(FlashTrackBarValueConverter)), _
Editor(GetType(FlashTrackBarValueEditor), _
GetType(UITypeEditor)), _
Description("The current value of the track bar. You can enter an actual value or a percentage.")> _
Public ReadOnly Property Value() As Integer
...
End Property
[
Category("Flash"),
TypeConverter(typeof(FlashTrackBarValueConverter)),
Editor(typeof(FlashTrackBarValueEditor), typeof(UITypeEditor)),
Description("The current value of the track bar. You can enter an actual value or a percentage.")
]
public int Value {
...
}
请参见
概念
使用 ShouldSerialize 和 Reset 方法定义默认值