我想使用Windows窗体创建一些东西来显示项目中的数据和有关子项的信息(= Parent - Childs)
--For exemple, i have
--MyParent with properties
MyParent.Data1
MyParent.Data2
MyParent.Data3
MyParent.Childs()
--And for child
MyChild.Data1
MyChild.Data2
MyChild.Data3
所以,在第一个Combobox中,我有我的项目列表, 在第二个中,我有来自当前所选父项的孩子
我对绑定父母没有任何问题,但那是孩子的另一个历史!
Public bs As BindingSource = New BindingSource()
[...]
bs.DataSource = New BindingList(Of FopEtatFichier)(DateMyData))
TxtData1.DataBindings.Add("Text", bs, "Data1", True, DataSourceUpdateMode.OnPropertyChanged)
TxtData2.DataBindings.Add("Text", bs, "Data2", True, DataSourceUpdateMode.OnPropertyChanged)
TxtData3.DataBindings.Add("Text", bs, "Data3", True, DataSourceUpdateMode.OnPropertyChanged)
ChkEtatSupprime.DataBindings.Add("Checked", bs, "EstSupprime", True, DataSourceUpdateMode.OnPropertyChanged)
我是否有义务为孩子创建另一个绑定源? 我做了一些非常丑陋的事情:
Private Sub CbbChild_SelectedValueChanged(sender As Object, e As EventArgs) Handles CbbChild.SelectedValueChanged, CbbChild.DataSourceChanged
ChildData1.DataBindings.Clear()
ChildData2.DataBindings.Clear()
ChildData3.DataBindings.Clear()
ChildData1.DataBindings.Add("Text", CbbChild.SelectedItem, "ChildData1", True, DataSourceUpdateMode.OnPropertyChanged)
ChildData2.DataBindings.Add("Checked", CbbChild.SelectedItem, "ChildData2", True, DataSourceUpdateMode.OnPropertyChanged)
ChildData3.DataBindings.Add("Text", CbbChild.SelectedItem, "ChildData3", True, DataSourceUpdateMode.OnPropertyChanged)
是否可以使用baove产品而不总是Clear / Add?
(如果您愿意,可以提供C#代码,无论如何)
感谢您的帮助!
答案 0 :(得分:1)
这个例子对我有用。创建一个WinForms应用项目,并将parentComboBox
,parentTextBox
,parentBindingSource
,childComboBox
,childTextBox
和childBindingSource
添加到表单中。
Public Class Form1
Private parents As New List(Of Parent) From {New Parent With {.ParentId = 1, .Name = "Parent 1"},
New Parent With {.ParentId = 2, .Name = "Parent 2"},
New Parent With {.ParentId = 3, .Name = "Parent 3"}}
Private children As New List(Of Child) From {New Child With {.ChildId = 1, .ParentId = 1, .Name = "Child 1"},
New Child With {.ChildId = 2, .ParentId = 2, .Name = "Child 2"},
New Child With {.ChildId = 3, .ParentId = 3, .Name = "Child 3"},
New Child With {.ChildId = 4, .ParentId = 1, .Name = "Child 4"},
New Child With {.ChildId = 5, .ParentId = 2, .Name = "Child 5"},
New Child With {.ChildId = 6, .ParentId = 3, .Name = "Child 6"},
New Child With {.ChildId = 7, .ParentId = 1, .Name = "Child 7"},
New Child With {.ChildId = 8, .ParentId = 2, .Name = "Child 8"},
New Child With {.ChildId = 9, .ParentId = 3, .Name = "Child 9"}}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Use a dummy list to enable configuring the bindings without any data.
childBindingSource.DataSource = New List(Of Child)
With childComboBox
.DisplayMember = "ChildId"
.ValueMember = "ChildId"
.DataSource = childBindingSource
End With
childTextBox.DataBindings.Add("Text", childBindingSource, "Name")
parentBindingSource.DataSource = parents
With parentComboBox
.DisplayMember = "ParentId"
.ValueMember = "ParentId"
.DataSource = parentBindingSource
End With
parentTextBox.DataBindings.Add("Text", parentBindingSource, "Name")
End Sub
Private Sub parentComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles parentComboBox.SelectedIndexChanged
Dim parentId = CInt(parentComboBox.SelectedValue)
'With this line, the first child will be selected after selecting a new parent.
'Without this line, the child SelectedIndex will remain unchanged even though the SelectedItem has changed.
childBindingSource.DataSource = New List(Of Child)
childBindingSource.DataSource = children.Where(Function(child) child.ParentId = parentId)
End Sub
End Class
Public Class Parent
Public Property ParentId As Integer
Public Property Name As String
End Class
Public Class Child
Public Property ChildId As Integer
Public Property ParentId As Integer
Public Property Name As String
End Class