使用自定义属性在PropertyGrid中禁用项目 [英] Disabling items in PropertyGrid using Custom Attributes
问题描述
通过将 BrowsableAttributes
设置为包含<$的数组,我可以有选择地启用/禁用 PropertyGrid
中的项目c $ c> CategoryAttribute 对象。但是,我希望启用某个类别中的某些项目,而禁用同一类别中的其他项目,因此我想创建自己的自定义属性类并将其应用于对象中的属性,但这似乎不起作用。 p>
这是我的自定义属性类:
公共枚举HeadType
DMA = 1
TMA = 2
两者都= 0
结束枚举
< AttributeUsage(AttributeTargets.Property)>
公共类HeadTypeAttribute
继承属性
公共属性HeadType作为HeadType
Public Sub New(HeadType作为HeadType)
Me.HeadType = HeadType
End Sub
公共重写函数Match(obj作为对象)为布尔值
Debug.Print( HeadTypeAttribute.Match obj.HeadType =& obj.HeadType.ToString())
Debug.Print( HeadTypeAttribute.Match Me.HeadType =& Me.HeadType.ToString())
Dim bMatch为布尔值= TypeOf obj是HeadTypeAttribute AndAlso CType(obj,HeadTypeAttribute).HeadType = Me.HeadType
调试。 Print(bMatch)
返回bMatch
结束函数
结束类
我将 BrowsableAttibutes
设置为一个包含两个HeadTypeAttribute类实例的数组,一个实例的HeadType = HeadType.Both,另一个设置为HeadType.DMA或HeadType.TMA。我可以看到Match方法正在被调用并且对于某些项目返回true,但是网格始终为空。
没有在BrowsableAttributes中添加Both项,并且将Match函数更改为在遇到Both值时始终返回True似乎可以解决问题:
公共重写函数Match(obj作为对象)作为布尔值
返回TypeOf obj是HeadTypeAttribute AndAlso(CType(obj,HeadTypeAttribute).HeadType = Me.HeadType OrElse CType(obj,HeadTypeAttribute).HeadType = HeadType.Both)
结束函数
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.
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
这篇关于使用自定义属性在PropertyGrid中禁用项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!