Create an expandable group in a property grid?(在属性网格中创建可扩展组?)
问题描述
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
这篇关于在属性网格中创建可扩展组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在属性网格中创建可扩展组?
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01