title: Listbox bound to ObservableCollection (in VB.NET and Silverlight)
body: <p>Sorry if this is a duplicate question.  There are quite a few questions around this topic, but I can't seem to get it figured out.  I'm trying to bind a listbox to an ObservableCollection and keep the listbox updated when items are added to the collection.</p>
  
  <p><strong>I have a class called CollectionOfBlogs:</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>Then another class where I setup the ObservableCollection of CollectionOfBlogs (above) and a sub to add items to the collection:</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>I bind the listbox to the blogNamesList collection in the ITRSBlogs Class (above) from my main page loaded event:</strong></p>
  
  <pre><code>Dim blogClass As New ITRSBlogs
  
  Me.BloggingMenuListBox.ItemsSource = blogClass.blogNamesList
  </code></pre>
  
  <p><strong>Here is the XAML of my listbox.  It's bound in the codebehind, not in the XAML (just thought I should mention that).</strong></p>
  
  <pre><code>&lt;ListBox Name="BloggingMenuListBox"/&gt;
  </code></pre>
  
  <p><strong>Before binding the collection to the listbox, I load the collection with items from the database, and they show up in the listbox just fine.  These 2 subs below are actually in the ITRSBlogs Class as well, and I call FillBlogLists from my pages loaded event.</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>Beyond this, I have a simple textbox and button on a page.  When a name is entered into the textbox, and the button is clicked, I call the addBlog(passing in name from textbox) sub routine in the ITRSBlogs Class (back up the page a bit) to add the item to the collection.</strong></p>
  
  <p><strong>Problem is, when I add an item to the collection, the listbox is not updated.  I'm new to Observable Collections (and many other things : ), so maybe I'm just really off here.  Can anyone tell me what I'm doing wrong?</strong></p>