在属性网格中创建可扩展组?

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

这篇关于在属性网格中创建可扩展组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)