【问题标题】:Make DataRepeater bound to List(Of Object) update?使 DataRepeater 绑定到 List(Of Object) 更新?
【发布时间】:2013-08-23 09:20:44
【问题描述】:

将 List(Of Object) 绑定到 DataRepeater 的正确方法是什么?你能提供这方面的示例代码吗?

我一直在为此绞尽脑汁,虽然我可以得到一个已经填满的列表以显示在转发器中,但对列表的后续更改对 DataRepeater 没有影响。

如果可能的话,我最终希望使用它来绑定字典,但我什至无法在这里使用基础知识。

在表单设计图面上添加了数据重复器,在 ItemTemplate 中带有 3 个标签和一个进度条。我尝试设置列表和中继器的代码(其中 DutData 是 DataRepeater)是:

Public Class BurnIn
    Public Shared currentDuts As New Dictionary(Of UInteger, DeviceUnderTest)   ' Collection of all current DUTs.
    Dim bs As New BindingSource
    Dim testTemp As Boolean = False
    Dim testList As New List(Of DeviceUnderTest)

    Private Sub BurnIn_Load() Handles Me.Load
        '...
        ' Add two items to the dictionary and populate them
        currentDuts.Add(0, New DeviceUnderTest(Me.user, 0))
        currentDuts.Item(0).RackBay = "012345678901"
        currentDuts.Item(0).AssemblySerial = "123456789"
        currentDuts.Item(0).SetProgram(1, "Program1")

        currentDuts.Add(currentDuts.Count, New DeviceUnderTest(Me.user, 1))
        currentDuts.Item(1).RackBay = "109876543210"
        currentDuts.Item(1).AssemblySerial = "1319A5126"
        currentDuts.Item(1).SetProgram(1, "Program1")
        ' Copy the items to the test list.
        testList.Add(currentDuts.Item(0))
        testList.Add(currentDuts.Item(1))
        testTemp = True

        ' Setup the binding source, data source and data bindings.
        bs.DataSource = testList
        LocationLabel.DataBindings.Add("Text", bs, "RackBay")
        DutLabel.DataBindings.Add("Text", bs, "AssemblySerial")
        ProgramLabel.DataBindings.Add("Text", bs, "Program")
        DutProgress.DataBindings.Add("Value", bs, "Progress")
        DutData.DataSource = testList
        '...
        Me.Show()
    End Sub

然后测试添加或删除列表项:

    Private Sub Button1_Click() Handles Button1.Click
        If testTemp = False Then
            ' Add an item to the dictionary and populate it.
            currentDuts.Add(currentDuts.Count, New DeviceUnderTest(Me.user, 1))
            currentDuts.Item(1).RackBay = "109876543210"
            currentDuts.Item(1).AssemblySerial = "1319A5126"
            currentDuts.Item(1).SetProgram(1, "Program1")
            ' Copy the item to the test list.
            testList.Add(currentDuts.Item(1))
            testTemp = True
        Else
            ' Remove the item from the dictionary and the list.
            currentDuts.Remove(1)
            testList.Remove(testList.Item(1))
            testTemp = False
        End If
    End Sub
End Class

【问题讨论】:

    标签: vb.net binding datarepeater


    【解决方案1】:

    第一件事是用 BindingList 替换您的 List

    Dim testList As New BindingList(Of DeviceUnderTest)
    

    【讨论】:

    • 太棒了,这行得通。然而有没有别的办法?例如,如果我想绑定到集合而不是 BindingCollection 是否提供等效功能?
    • 是的,BindingX 对象实现了成功绑定 InotifyCollectionChanged 和 INotifyPropertyChanged IIRC 所需的接口
    猜你喜欢
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    相关资源
    最近更新 更多