【发布时间】:2011-01-09 05:08:05
【问题描述】:
对不起,如果这是一个重复的问题。围绕这个主题有很多问题,但我似乎无法弄清楚。我正在尝试将列表框绑定到 ObservableCollection 并在将项目添加到集合时保持列表框更新。
我有一个名为 CollectionOfBlogs 的课程:
Public Class CollectionOfBlogs
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Public Sub New(ByVal name As String)
Me.FullName = name
End Sub
Private _FullName As String
Public Property FullName() As String
Get
Return _FullName
End Get
Set(ByVal value As String)
_FullName = value
NotifyPropertyChanged("FullName")
End Set
End Property
Public Sub NotifyPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
然后是另一个类,我设置了 CollectionOfBlogs 的 ObservableCollection(上图)和一个子类以将项目添加到集合中:
Public Class ITRSBlogs
Public blogNamesList As New ObservableCollection(Of CollectionOfBlogs)
Public Sub addBlog(ByVal FullName as String)
blogNamesList.Add(New CollectionOfBlogs(FullName))
End Sub
End Class
我通过主页加载事件将列表框绑定到 ITRSBlogs 类(上图)中的 blogNamesList 集合:
Dim blogClass As New ITRSBlogs
Me.BloggingMenuListBox.ItemsSource = blogClass.blogNamesList
这是我的列表框的 XAML。它绑定在代码隐藏中,而不是 XAML 中(只是想我应该提一下)。
<ListBox Name="BloggingMenuListBox"/>
在将集合绑定到列表框之前,我使用数据库中的项目加载集合,它们会很好地显示在列表框中。下面这两个子实际上也在 ITRSBlogs 类中,我从我的页面加载事件中调用 FillBlogLists。
Public Sub FillBlogLists()
Dim query = theContext.GetBlogsOrderedByNameQuery
theContext.Load(query, AddressOf OnBlogsLoaded, Nothing)
End Sub
Private Sub OnBlogsLoaded(ByVal lo As LoadOperation(Of Blog))
blogList.Clear()
For Each s In lo.AllEntities
blogList.Add(CType(s, Blog))
Next
For Each item In blogList
blogNamesList.Add(New CollectionOfBlogs(item.FullName))
Next
End Sub
除此之外,我在页面上有一个简单的文本框和按钮。当在文本框中输入名称并单击按钮时,我会调用 ITRSBlogs 类中的 addBlog(passing in name from textbox) 子例程(稍微备份页面)以将项目添加到集合中。强>
问题是,当我将一个项目添加到集合中时,列表框没有更新。我是 Observable Collections 的新手(还有很多其他的东西 : ),所以也许我真的不在这里。谁能告诉我我做错了什么?
【问题讨论】:
标签: asp.net wpf vb.net silverlight-4.0