英文:
DataListView with DataSource of ObservableCollection does not display newly added objects
问题
-
创建了一个包含单个按钮和单个DataListView的表单。
-
在表单中添加了以下代码:
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
-
运行应用程序,点击按钮。在调试器中,我可以看到一个新条目被添加到ObservableCollection,并相应地显示在DataListView的对象中。
-
该项目从未显示在GUI中(是的,DataListView的唯一列在设计时已经设置为"X"属性名)。
这实际上是我在一个较大的应用程序中遇到的问题的一个简化版本。https://stackoverflow.com/a/30157854/2112855 中给出的答案暗示这应该 "只需工作"。这是一个缺陷,还是我操作不当?
英文:
-
Created form with a single button and a single DataListView.
-
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
- 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:
- Item never appears in the GUI (Yes, the DataListView's sole column is already set up with the aspect name "X" at design time.)
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论