绑定PropertyGrid

时间:2019-02-22 22:45:53

标签: binding propertygrid

关于绑定PropertyGrid几乎没有,因此我想寻求一些指导。这是一个示例应用程序:

enter image description here

它包含一个简单的数据表:

    If dt.Columns.Count = 0 Then
        dt.Columns.Add("ID", GetType(Int32))
        dt.Columns.Add("MyString", GetType(String))
        dt.Columns.Add("MyCustomEnum", GetType(Int16))
    End If
    dt.Rows.Add({1, "Item One", 4})
    dt.Rows.Add({2, "Item Two", 16})
    dt.Rows.Add({3, "Item Three", 32})
    dt.Rows.Add({4, "Item Four", 4})
    dt.Rows.Add({5, "Item Five", 1})

绑定到DataGridView1NumericUpDown1TextBox1ComboBox1

    bs.DataSource = dt
    Me.dataGridView1.DataSource = bs

    Me.NumericUpDown1.DataBindings.Add("Value", bs, "ID")
    Me.textBox1.DataBindings.Add("Text", bs, "MyString")
    Me.ComboBox1.DataSource = CreateTableFromEnum(GetType(DataItem.GeneralAlignment))
    Me.ComboBox1.ValueMember = "key"
    Me.ComboBox1.DisplayMember = "text"
    Me.ComboBox1.DataBindings.Add("SelectedValue", bs, "MyCustomEnum")

那只是为了说明。我创建了一个具有PropertyGrid1属性的类:

<DefaultPropertyAttribute("DataItem")> Public Class DataItem
Public ID As Int32
Public MyString As String
Public MyCustomEnum As GeneralAlignment

Public Sub New(nID As Int32, nMyString As String, nMyCustomEnum As GeneralAlignment)
    ID = nID
    MyString = nMyString
    MyCustomEnum = MyCustomEnum
End Sub

<CategoryAttribute("General Properties"), Browsable(True), [ReadOnly](False), BindableAttribute(False), DefaultValueAttribute("0"), DesignOnly(False), DescriptionAttribute("ID"), DisplayName("ID")>
Public Property PropID() As Int32
    Get
        Return ID
    End Get
    Set(ByVal Value As Int32)
        ID = Value
    End Set
End Property

<CategoryAttribute("General Properties"), Browsable(True), [ReadOnly](False), BindableAttribute(False), DefaultValueAttribute(""), DesignOnly(False), DescriptionAttribute("Database datatype"), DisplayName("MyString")>
Public Property PropDataType() As String
    Get
        Return MyString
    End Get
    Set(ByVal Value As String)
        MyString = Value
    End Set
End Property

<CategoryAttribute("General Properties"), DefaultValueAttribute(""), DescriptionAttribute("Select column alignment"), DisplayName("MyCustomEnum")>
Public Property PropMyCustomEnum() As GeneralAlignment
    Get
        Return MyCustomEnum
    End Get
    Set(ByVal Value As GeneralAlignment)
        MyCustomEnum = Value
    End Set
End Property

Public Enum GeneralAlignment
    NotSet = 0
    TopLeft = 1
    TopCenter = 2
    TopRight = 4
    MiddleLeft = 16
    MiddleCenter = 32
    MiddleRight = 64
    BottomLeft = 256
    BottomCenter = 512
    BottomRight = 1024
End Enum

End Class

为了进行演示,我通过以下方式将一些示例数据加载到PropertyGrid1中:

Dim Prop As New DataItem(16, "Item Two", DataItem.GeneralAlignment.MiddleLeft)
Private Sub TestProps()
    Me.propertyGrid1.PropertySort = PropertySort.Categorized
    Me.propertyGrid1.SelectedObject = Prop
End Sub

但是如何绑定PropertyGrid1 以显示BindingSource bs的货币管理器中的一行?即显示数据表的选定行详细信息。

0 个答案:

没有答案