DataListView 使用 ObservableCollection 作为数据源时,不显示新添加的对象。

huangapple go评论63阅读模式
英文:

DataListView with DataSource of ObservableCollection does not display newly added objects

问题

  1. 创建了一个包含单个按钮和单个DataListView的表单。

  2. 在表单中添加了以下代码:

Dim dummies As New ObservableCollection(Of Dummy)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    dlvDummies.DataSource = dummies
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    dummies.Add(New Dummy(Now.ToLongTimeString()))
End Sub

Class Dummy
    Public Property X As String
    Sub New(x As String)
        Me.X = x
    End Sub
End Class
  1. 运行应用程序,点击按钮。在调试器中,我可以看到一个新条目被添加到ObservableCollection,并相应地显示在DataListView的对象中。

  2. 该项目从未显示在GUI中(是的,DataListView的唯一列在设计时已经设置为"X"属性名)。

这实际上是我在一个较大的应用程序中遇到的问题的一个简化版本。https://stackoverflow.com/a/30157854/2112855 中给出的答案暗示这应该 "只需工作"。这是一个缺陷,还是我操作不当?

英文:
  1. Created form with a single button and a single DataListView.

  2. Added this code to form:

    Dim dummies As New ObservableCollection(Of Dummy)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dlvDummies.DataSource = dummies
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        dummies.Add(New Dummy(Now.ToLongTimeString()))
    End Sub

    Class Dummy
        Public Property X As String
        Sub New(x As String)
            Me.X = x
        End Sub
    End Class
  1. Ran the app, pressed the button. In the debugger, I can visibly see a new entry added to the ObservableCollection and correspondingly to the DataListView .Objects:

DataListView 使用 ObservableCollection 作为数据源时,不显示新添加的对象。

  1. Item never appears in the GUI (Yes, the DataListView's sole column is already set up with the aspect name "X" at design time.)

DataListView 使用 ObservableCollection 作为数据源时,不显示新添加的对象。

This is actually a bare bones reproduction of the problem I'm having in a larger app. The answer given in https://stackoverflow.com/a/30157854/2112855 implies that this should "just work". Is this a defect, or user error on my part?

答案1

得分: 0

替换 ObservableCollection(Of foo) 为 BindingList(Of foo) 可以解决这个问题,或者说是一种解决方法。

我认为,基于在 https://sourceforge.net/p/objectlistview/discussion/812923/thread/ab0d6208/ 上看到的讨论,https://stackoverflow.com/a/30157854/2112855 中的被接受的答案是有问题的,可能需要编辑。

英文:

Replacing ObservableCollection(Of foo) with BindingList(Of foo) fixed, or rather worked around this.

I think, based on the discussion seen at https://sourceforge.net/p/objectlistview/discussion/812923/thread/ab0d6208/ that the accepted answer in https://stackoverflow.com/a/30157854/2112855 is faulty and should probably be edited.

huangapple
  • 本文由 发表于 2020年1月7日 00:30:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定