Сделать DataRepeater привязанным к обновлению List(Of Object)?

Каков правильный способ привязки списка (объекта) к DataRepeater? Можете ли вы предоставить пример кода для этого?

Я ломал голову над этим, и хотя я могу получить уже заполненный список для отображения в повторителе, последующие изменения в списке не влияют на DataRepeater.

В конечном счете, я надеюсь использовать это для привязки к словарю, если это возможно, но я не могу заставить работать даже основы.

Повторитель данных добавляется на поверхность конструктора формы с 3 метками и индикатором выполнения в ItemTemplate. Код, который я попытался (где 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

person Toby    schedule 23.08.2013    source источник


Ответы (1)


Прежде всего, замените свой список на BindingList.

Dim testList As New BindingList(Of DeviceUnderTest)
person peterG    schedule 23.08.2013
comment
Блестяще, это работает. Однако есть ли другой способ? Например, если я хочу привязаться к коллекции, предоставляет ли BindingCollection эквивалентную функциональность? - person Toby; 23.08.2013
comment
Да, объекты BindingX реализуют интерфейсы, необходимые для успешной привязки InotifyCollectionChanged и INotifyPropertyChanged IIRC. - person peterG; 23.08.2013