自定义控件中的组属性

时间:2022-09-02 13:24:56

In our IDE, for example, Visual Studio, if we display the properties of a System.Windows.Forms.Button control, we see some properties that expose anoter set of properties. For example: FlatAppearance, Font, Location, Margin, etcetera.

在我们的IDE中,例如,Visual Studio,如果我们显示System.Windows.Forms.Button控件的属性,我们会看到一些公开一组属性的属性。例如:FlatAppearance,Font,Location,Margin等。

I would like to do something similar in a custom control.

我想在自定义控件中做类似的事情。

I know the code behind is wrong, but here is an example of what I´m trying to do:

我知道背后的代码是错误的,但这里是我尝试做的一个例子:

Public Class StateOfMyCustomControl

    Public Enum EnumVisibility
        Visible
        NonVisible
    End Enum

    Public Enum EnumEventManagement
        Automatic
        Manual
    End Enum

    Private mAssociatedControl As MyCustomControl
    Private mVisibility As EnumVisibility
    Private mEventManagement As EnumEventManagement

    Public Sub New(ByVal AssociatedControl As MyCustomControl)
        mAssociatedControl = AssociatedControl
    End Sub

    Public Property Visibility() As EnumVisibility
        Get
            Return mVisibility
        End Get
        Set(ByVal value As EnumVisibility)

            mVisibility = value

            mAssociatedControl.Visible = False
            If mVisibility = EnumVisibility.Visible Then
                mAssociatedControl.Visible = True
            End If

        End Set
    End Property

    Public Property EventManagement() As EnumEventManagement
        Get
            Return mEventManagement
        End Get
        Set(ByVal value As EnumEventManagement)
            mEventManagement = value
        End Set
    End Property

End Class

Public Class MyCustomControl

    ' ...

    Private mState As StateOfMyCustomControl

    Public Sub New()
        mState = New StateOfMyCustomControl(Me)
    End Sub

    Public Property State() As StateOfMyCustomControl
        Get
            Return mState
        End Get
        Set(ByVal value As StateOfMyCustomControl)
            mState = value
        End Set
    End Property

    ' ...

End Class

In my IDE, in the properties window of my custom control, I would like to see my property State, with the possibility of display it to set the properties Visibility and EventManagement.

在我的IDE中,在我的自定义控件的属性窗口中,我希望看到我的属性State,可以显示它来设置属性Visibility和EventManagement。

Thanks very much

非常感谢

1 个解决方案

#1


You need to tell it to use ExpandableObjectConverter (or a custom converter) for StateOfMyCustomControl. In C#, this is:

您需要告诉它使用ExpandableObjectConverter(或自定义转换器)进行StateOfMyCustomControl。在C#中,这是:

[TypeConverter(typeof(ExpandableObjectConverter))]
public class StateOfMyCustomControl {...}

However you apply attributes in VB, do that ;-p

但是,您在VB中应用属性,请执行此操作;-p

Possibly:

<TypeConverter(GetType(ExpandableObjectConverter))> _
Public Class StateOfMyCustomControl
...

#1


You need to tell it to use ExpandableObjectConverter (or a custom converter) for StateOfMyCustomControl. In C#, this is:

您需要告诉它使用ExpandableObjectConverter(或自定义转换器)进行StateOfMyCustomControl。在C#中,这是:

[TypeConverter(typeof(ExpandableObjectConverter))]
public class StateOfMyCustomControl {...}

However you apply attributes in VB, do that ;-p

但是,您在VB中应用属性,请执行此操作;-p

Possibly:

<TypeConverter(GetType(ExpandableObjectConverter))> _
Public Class StateOfMyCustomControl
...