如何在 Windows 表单的属性网格中隐藏展开按钮

How to hide expand button in a propertygrid in Windows Forms

我已经在 属性Grid 中为 属性 编写了编辑器。一切都按预期工作,但为了获得最佳用户体验,我想为此 属性 隐藏 属性 网格上的展开按钮。谁能告诉我该怎么做?

我正在使用自定义类型转换器

Public Class PropertyLanguageSetupEditor
    Inherits UITypeEditor

    Public Overrides Function GetEditStyle(context As ITypeDescriptorContext) As UITypeEditorEditStyle
        Return UITypeEditorEditStyle.Modal
    End Function

    Public Overrides Function EditValue(context As ITypeDescriptorContext, provider As IServiceProvider, value As Object) As Object
        Try
            Dim svc As IWindowsFormsEditorService = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)

            Dim settings As LanguageSettings = value
            Dim oldSettings As LanguageSettings = CType(value, LanguageSettings).Clone

            If Not svc Is Nothing And Not settings Is Nothing Then
                Dim form As New PropertyLanguageSetupWindow(settings)
                If svc.ShowDialog(form) = DialogResult.OK Then
                    value = form.Data
                Else
                    value = oldSettings
                End If
            End If
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try
        Return MyBase.EditValue(context, provider, value)
    End Function
End Class

这是使用 typeConverter

的 属性
   <DisplayName("Properties Translation Settings"), Editor(GetType(PropertyLanguageSetupEditor), GetType(System.Drawing.Design.UITypeEditor)), TypeConverter(GetType(BrowsableTypeConverter)),
        Description("Settings for the output text"), BrowsableTypeConverter.BrowsableLabelStyleAttribute(BrowsableTypeConverter.LabelStyle.lsEllipsis),
        Category("10 - DXF Export"), Browsable(True)>
    Public Property TranslationSettings As LanguageSettings = New LanguageSettings

我的 BrowsAbleTypeConverter

Public Class BrowsableTypeConverter
    Inherits ExpandableObjectConverter

    Public Enum LabelStyle
        lsNormal
        lsTypeName
        lsEllipsis
        lsText
    End Enum


    Public Class BrowsableLabelStyleAttribute
        Inherits System.Attribute
        Private eLabelStyle As LabelStyle = LabelStyle.lsEllipsis
        Public Sub New(ByVal LabelStyle As LabelStyle)
            eLabelStyle = LabelStyle
        End Sub
        Public Property LabelStyle() As LabelStyle
            Get
                Return eLabelStyle
            End Get
            Set(ByVal value As LabelStyle)
                eLabelStyle = value
            End Set
        End Property
    End Class

    Public Class BrowsableLabelTextAttribute
        Inherits System.Attribute
        Private strText As String = ""
        Public Sub New(ByVal value As String)
            strText = value
        End Sub
        Public Property Text() As String
            Get
                Return strText
            End Get
            Set(ByVal value As String)
                strText = value
            End Set
        End Property
    End Class

    Public Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
        Return True
    End Function

    Public Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
        Dim Style As BrowsableLabelStyleAttribute = context.PropertyDescriptor.Attributes(GetType(BrowsableLabelStyleAttribute))
        If Not Style Is Nothing Then
            Select Case Style.LabelStyle
                Case LabelStyle.lsNormal
                    Return MyBase.ConvertTo(context, culture, value, destinationType)
                Case LabelStyle.lsTypeName
                    Return "(" & value.GetType.Name & ")"
                Case LabelStyle.lsEllipsis
                    Return "(...)"
                Case LabelStyle.lsText
                    Dim text As BrowsableLabelTextAttribute = context.PropertyDescriptor.Attributes(GetType(BrowsableLabelTextAttribute))
                    If text IsNot Nothing Then
                        Return text.Text
                    End If
            End Select
        End If
        Return MyBase.ConvertTo(context, culture, value, destinationType)
    End Function


End Class

您的 TypeConverter 派生自 ExpandableObjectConverter, which means subproperties will be visible because of its GetPropertiesSupported 方法 return 为真。

在您的 TypeConverter 中,您只需覆盖 GetPropertiesSupported 和 return false。