【问题标题】:PropertyGrid ExpandableObjectConverter Collection property List(Of T) with constructor and ReadOnly property具有构造函数和 ReadOnly 属性的 PropertyGrid ExpandableObjectConverter 集合属性 List(Of T)
【发布时间】: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


    【解决方案1】:

    好吧,看来我可能是在陈述显而易见的答案,因为我猜测显而易见的答案总是会被忽视。我不得不将我的列表公开为全局变量。所以在我的代码中我说:

    Class MyClass
        Private _Parent As MyCollection = Nothing
        ...
        Public Sub New()
        End Sub
    
        Private ReadOnly Property Parent As MyCollection
            Get
                If _Parent Is Nothing Then
                    _Parent = Module1.GlobalParent
                End If
                Return _Parent
            End Get
        End Prpoerty
        ...
        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
    

    即毕竟不需要自定义 TypeConverter,幸运的是我的模块、表单和类在同一个项目中,否则生活会很困难!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-05
      相关资源
      最近更新 更多