PropertyGrid控件(8) 下拉框属性

概要:将 PropertyGrid 的某个属性,做成下拉框显示,可以用用户在下拉框里选择值。

Adding Domain List and Simple Drop-down Property Support

For properties that return an enumeration based upon the Enum type, the PropertyGrid automatically displays the enumeration values in a drop-down list. The EnumConverter also provides this functionality. For you own properties, you might want to provide a list of valid values to the user, sometimes called a pick list or domain list, with types not based upon Enum. This is the case when the domain values are not known until run time, or when the values can change.

Modify the options window to provide a domain list of default file names that the user can choose from. You have already added the DefaultFileName property to the AppSettings class. The next step is to display the drop-down for the property in the PropertyGrid, in order to provide the domain list.

To provide simple drop-down property support

1. Create a class that inherits from a type converter class. Since the DefaultFileName property is of String type, you can inherit from StringConverter. If a type converter for the type of your property doesn't exist, you can inherit from TypeConverter; in this case it isn't necessary.

Public Class FileNameConverter
Inherits StringConverter
End Class

2. Override the GetStandardValuesSupported method and return true to indicate that this object supports a standard set of values that can be picked from a list.

Public Overloads Overrides Function GetStandardValuesSupported( _
ByVal context As ITypeDescriptorContext) As Boolean
Return True
End Function

3.Override the GetStandardValues method and return a StandardValuesCollection filled with your standard values. One way to create a StandardValuesCollection is to provide an array of values in the constructor. For the options window application you can use a String array filled with proposed default file names.

Public Overloads Overrides Function GetStandardValues( _
ByVal context As ITypeDescriptorContext) _
As StandardValuesCollection Return New StandardValuesCollection(New String() {"New File", _
"File1", _
"Document1"})
End Function

4. (Optional) If you want the user to be able to type in a value that is not in the drop-down list, override the GetStandardValuesExclusive method and return false. This basically changes the drop-down list style to a combo box style.

Public Overloads Overrides Function GetStandardValuesExclusive( _
ByVal context As ITypeDescriptorContext) As Boolean
Return False
End Function

5. Now that you have your own type converter class for displaying a drop-down list, you need to identify the target that will use it. In this case the target is the DefaultFileName property, since the type converter is specific to the property. Apply the TypeConverterAttribute to the target property.

' The TypeConverter attribute applied to the DefaultFileName property.
<TypeConverter(GetType(FileNameConverter)), _
CategoryAttribute("Document Settings")> _
Public Property DefaultFileName() As String
Get
Return _defaultFileName
End Get
Set(ByVal Value As String)
_defaultFileName = Value
End Set
End Property

Compile and run the options window application again. The following screen shot shows how the options window should now look. Notice how the DefaultFileName property displays.

propertygrid, drop-down list style property

Figure 7. Displaying a drop-down domain list in the PropertyGrid

编者或作者: 我有闲    收录日期: 2008-12-22
参考或来源: 我有闲IT教程网 WoYouXian.net

上一页: PropertyGrid控件(7) 扩展属性 返回上级目录: IT知识文章 下一页: PropertyGrid控件(9) 自定义 UITypeEditor


© 2008 woyouxian.net 版权所有 Contact Us