【发布时间】:2015-12-09 07:25:09
【问题描述】:
我有一个在通用列表中使用的类型,它有一个构造函数参数,即列表本身。 例如
Class MyCollection(Of T)
Inherits List(Of T)
...
End Class
用于 T 的实际类声明为:
Class MyClass
Private _Parent As MyCollection
Private _Property1 As String
Private _Property2 As String
Public Sub New(Parent As MyCollection, Property1 As String)
_Parent = Parent
Me.Property1 = Property1
End Sub
...
Public Property Property1 As String
Get
Return _Property1
End Get
Set(value As String)
_Property1 = value
Dim result As String = value
For Each item in Parent
If item Is Me Then
Exit For
End If
...
Next
_Property2 = result
End Set
End Property
Public ReadOnly Property Property2 As String
Get
Return _Property2
End Get
End Property
End Class
如何实现这样的 TypeConvertor 以在 PropertyGrid 中使用。 我的 Parent 属性没有公开,因为除了在类本身之外我不使用它,并且我的 Property2 是只读的。因此,在运行时的 PropertyGrid 设计器中,我不想看到 Parent,我确实想看到 Property2,但它应该是只读的。如果我根本不使用 TypeConverter 就是这种情况,那么为什么当我使用 TypeConverter 时它不能像我想要的那样工作。如果没有类型转换器,则添加新项目不能正常工作,至少我收到一条错误消息“未找到类型‘MyClass’的构造函数”,所以我认为我需要重写 CreateInstance 和 GetCreateInstanceSupported 方法。即使我为我的 Parent 属性创建了一个 Property 并将 Browsable 属性设置为 False,那么我的 CreateInstance 方法也无法找到 Parent,因为它不在 propertyValues 中。如果改为公开它并设置 ReadOnly 属性,它会被看到并且它也是我不想要的可编辑的。我的 Property2 也是如此,我不希望它在 PropertyGrid 中可编辑。 ReadOnly 似乎被忽略了。我的 MyCollection 类还需要 TypeConverter 还是需要覆盖其他方法??!!
【问题讨论】:
标签: vb.net typeconverter