问题描述
SCENARIO
I've sub-classed a ListBox
and I've added color properties for when my control is Enabled, Disabled, or in ReadOnly mode:
QUESTION
In C# or VB.Net, what I need to do to organize the properties in the property grid into expandable groups with this structure?:
[+] State Enabled
[+] Selected Item
· BackColor
· ForeColor
[+] Unselected Item
· BackColor
· ForeColor
This is a visual example taken from a Krypton
lib user control that demonstrates what I would like to mimic:
UPDATE
I think that in this url is explained all the necessary about property grids:
http://www.codeproject.com/Articles/2764/Using-PropertyGrid-Part-I
But it is focused to do it with form which has a Load event, I still can't understand how to implement that example in my user control because If I create a sub-class to customize the property grid like in that example then I can't acces to my control's base class.
My code has a simple structure like this:
Public Class ElektroListBox : Inherits ListBox
<Category("Appearance")>
<Description("The BackColor to paint the selected item when the control is enabled.")>
Public Property StateEnabledItemSelectedBackColor As Color
Get
Return Me.stateEnabledItemSelectedBackColor1
End Get
Set(ByVal value As Color)
Me.stateEnabledItemSelectedBackColor1 = value
Me.Invalidate(invalidateChildren:=False)
End Set
End Property
Private stateEnabledItemSelectedBackColor1 As Color = Color.Red
End Class
The first thing you need to do is to structure your properties into classes. Then you need to create a custom type converter for each class so it becomes serializable. However, there's an easier way to achieve this; inherit Component class. Here's a simple example.
Public Class UIListBox
Inherits ListBox
Public Sub New()
Me.m_stateDisabled = New ItemLayout(Me)
Me.m_stateEnabled = New ItemLayout(Me)
Me.m_stateReadOnly = New ItemLayout(Me)
End Sub
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public ReadOnly Property StateDisabled() As ItemLayout
Get
Return Me.m_stateDisabled
End Get
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public ReadOnly Property StateEnabled() As ItemLayout
Get
Return Me.m_stateEnabled
End Get
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public ReadOnly Property StateReadOnly() As ItemLayout
Get
Return Me.m_stateReadOnly
End Get
End Property
Friend Sub NotifyStateChanged(source As ItemLayoutColors, propertyName As String)
Me.Invalidate()
Debug.WriteLine("UIListBox: State changed.")
End Sub
Private m_stateDisabled As ItemLayout
Private m_stateEnabled As ItemLayout
Private m_stateReadOnly As ItemLayout
End Class
<ToolboxItem(False)>
Public Class ItemLayout
Inherits Component
Public Sub New(listBox As UIListBox)
Me.m_listBox = listBox
Me.m_background = New ItemLayoutColors(Me)
Me.m_foreground = New ItemLayoutColors(Me)
End Sub
Friend ReadOnly Property ListBox() As UIListBox
Get
Return Me.m_listBox
End Get
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public ReadOnly Property Background() As ItemLayoutColors
Get
Return Me.m_background
End Get
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public ReadOnly Property Foreground() As ItemLayoutColors
Get
Return Me.m_foreground
End Get
End Property
Private m_background As ItemLayoutColors
Private m_foreground As ItemLayoutColors
Private m_listBox As UIListBox
End Class
<ToolboxItem(False)>
Public Class ItemLayoutColors
Inherits Component
Public Sub New(layout As ItemLayout)
If (layout Is Nothing) Then Throw New ArgumentNullException("layout")
Me.m_layout = layout
End Sub
Friend ReadOnly Property Layout() As ItemLayout
Get
Return Me.m_layout
End Get
End Property
Public Property Selected() As Color
Get
Return Me.m_selected
End Get
Set(value As Color)
If (value <> Me.m_selected) Then
Me.m_selected = value
Me.Layout.ListBox.NotifyStateChanged(Me, "Selected")
End If
End Set
End Property
Public Property Unselected() As Color
Get
Return Me.m_unselected
End Get
Set(value As Color)
If (value <> Me.m_unselected) Then
Me.m_unselected = value
Me.Layout.ListBox.NotifyStateChanged(Me, "Unselected")
End If
End Set
End Property
Private Function ShouldSerializeSelected() As Boolean
Return (Me.Selected <> Color.Empty)
End Function
Private Function ShouldSerializeUnselected() As Boolean
Return (Me.Unselected <> Color.Empty)
End Function
Private m_selected As Color
Private m_unselected As Color
Private m_layout As ItemLayout
End Class
Designer file
Me.UiListBox1.StateDisabled.Background.Selected = System.Drawing.Color.Red
这篇关于在属性网格中创建可扩展组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!