这是我涉及LiteDB的实际案例的摘要。它将问题简化为更广泛的.net问题。
假设我有3个班级:
Public Class Control
Public Property ID As Integer
Public Property ControlName As String
End Class
Public Class Controller
Public Property ID As Integer
Public Property ControllerName As String
Public Controls As List(Of Control)
End Class
Public Class Console
Public Property ID As Integer
Public Property ConsoleName As String
Public Controllers As List(Of Controller)
End Class
我通过Controller
设计器将Console
和BindingSources
映射到两个单独的Data Source
。
我有一个Custom Control
,其中包含一个ComboBox
。对于同时编辑控制器和控制台列表的页面,此实例绑定到List(Of Control)
或List(Of Controller)
上。
现在,我希望自定义控件更新Controller和Console中的相关属性,并向其传递属性ID
,我希望使用组合框的SelectedValue
进行更新。 Hopefull,下面的伪代码说明了这一点:
'Custom Control with ComboBox:
Public Class UserControl
Public bs As BindingSource ' Can contain rows of Controller or Console
Public CollectionName As String
Dim WithEvents KComboBox As ComboBox
Private Sub ComboChanged() Handles KComboBox.SelectionChangeCommitted
' [PseudoCode]
DirectCast(bs.Current, Controller).[CollectionName].ID = KComboBox.SelectedValue
' Resolves to:
DirectCast(bs.Current, Controller).[Controls.ID] = KComboBox.SelectedValue
' or:
DirectCast(bs.Current, Console).[Controllers.ID] = KComboBox.SelectedValue
End Sub
End Class
我将这样设置自定义组合框:
Dim cbControls As New UserControl With {.bs = ConrollerBS, .CollectionName = "Controls"}
Dim cbControllers As New UserControl With {.bs = ConsoleBS, .CollectionName = "Controllers"}
我认为这可能涉及Reflection
?我试图继续阅读,但是它使我的头顶掉了下来。
如何实现以上目标?