问题
I am able to selectively enable / disable items in a PropertyGrid
by setting BrowsableAttributes
to an array containing CategoryAttribute
objects. However, I wish to enable some items within a category and disable others within the same category, so I thought I would create my own custom attribute class and apply this to the properties in my object, but this does not seem to work.
Here is my custom attribute class:
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
I set BrowsableAttibutes
to an array containing two instances of my HeadTypeAttribute class, one with HeadType = HeadType.Both and one set to either HeadType.DMA or HeadType.TMA. I can see that the Match method is being called and is returning true for some items, but the grid is always empty.
回答1:
Not adding the Both item in BrowsableAttributes and changing the Match function to always return True when the Both value is encountered seems to fix it:
Public Overrides Function Match(obj As Object) As Boolean
Return TypeOf obj Is HeadTypeAttribute AndAlso (CType(obj, HeadTypeAttribute).HeadType = Me.HeadType OrElse CType(obj, HeadTypeAttribute).HeadType = HeadType.Both)
End Function
来源:https://stackoverflow.com/questions/28761295/disabling-items-in-propertygrid-using-custom-attributes