【发布时间】:2015-04-29 22:36:52
【问题描述】:
我可以通过将BrowsableAttributes 设置为包含CategoryAttribute 对象的数组来选择性地启用/禁用PropertyGrid 中的项目。但是,我希望启用一个类别中的某些项目并禁用同一类别中的其他项目,所以我想我会创建自己的自定义属性类并将其应用于我的对象中的属性,但这似乎不起作用。
这是我的自定义属性类:
Public Enum HeadType
DMA = 1
TMA = 2
Both = 0
End Enum
<AttributeUsage(AttributeTargets.Property)>
Public Class HeadTypeAttribute
Inherits Attribute
Public Property HeadType As HeadType
Public Sub New(HeadType As HeadType)
Me.HeadType = HeadType
End Sub
Public Overrides Function Match(obj As Object) As Boolean
Debug.Print("HeadTypeAttribute.Match obj.HeadType=" & obj.HeadType.ToString())
Debug.Print("HeadTypeAttribute.Match Me.HeadType=" & Me.HeadType.ToString())
Dim bMatch As Boolean = TypeOf obj Is HeadTypeAttribute AndAlso CType(obj, HeadTypeAttribute).HeadType = Me.HeadType
Debug.Print(bMatch)
Return bMatch
End Function
End Class
我将BrowsableAttibutes 设置为一个数组,其中包含我的 HeadTypeAttribute 类的两个实例,一个是 HeadType = HeadType.Both,一个设置为 HeadType.DMA 或 HeadType.TMA。我可以看到 Match 方法正在被调用并且对某些项目返回 true,但网格始终为空。
【问题讨论】:
-
试验了一下,看来自定义属性类可能没什么问题。只是如果您在 BrowsableAttributes 中设置多个值,则属性必须具有 ALL 而不是这些属性中的任何一个才能显示。
标签: .net custom-attributes propertygrid