title: How to Implement a ListBox of Checkboxes in WPF?
body: <p>Although somewhat experienced with writing Winforms applications, the... "vagueness" of WPF still eludes me in terms of best practices and design patterns.</p>
  
  <p>Despite populating my list at runtime, my listbox appears empty.</p>
  
  <p>I have followed the simple instructions from <a href="http://merill.net/2009/10/wpf-checked-listbox/" rel="noreferrer">this helpful article</a> to no avail. I suspect that I'm missing some sort of <code>DataBind()</code> method where I tell the listbox that I'm done modifying the underlying list.</p>
  
  <p>In my MainWindow.xaml, I have:</p>
  
  <pre><code>    &lt;ListBox ItemsSource="{Binding TopicList}" Height="177" HorizontalAlignment="Left" Margin="15,173,0,0" Name="listTopics" VerticalAlignment="Top" Width="236" Background="#0B000000"&gt;
          &lt;ListBox.ItemTemplate&gt;
              &lt;HierarchicalDataTemplate&gt;
                  &lt;CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/&gt;
              &lt;/HierarchicalDataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;
      &lt;/ListBox&gt;
  </code></pre>
  
  <p>In my code-behind, I have:</p>
  
  <pre><code>    private void InitializeTopicList( MyDataContext context )
      {
          List&lt;Topic&gt; topicList = ( from topic in context.Topics select topic ).ToList();
  
          foreach ( Topic topic in topicList )
          {
              CheckedListItem item = new CheckedListItem();
              item.Name = topic.DisplayName;
              item.ID = topic.ID;
              TopicList.Add( item );
          }
      }
  </code></pre>
  
  <p>Which, by tracing through, I know is being populated with four items.</p>
  
  <p><b>EDIT</b></p>
  
  <p>I have changed <code>TopicList</code> to an <code>ObservableCollection</code>. It still doesn't work.</p>
  
  <pre><code>    public ObservableCollection&lt;CheckedListItem&gt; TopicList;
  </code></pre>
  
  <p><b>EDIT #2</b></p>
  
  <p>I have made two changes that help:</p>
  
  <p>In the .xaml file:</p>
  
  <pre><code>ListBox ItemsSource="{Binding}"
  </code></pre>
  
  <p>In the source code after I populate the list:</p>
  
  <pre><code>listTopics.DataContext = TopicList;
  </code></pre>
  
  <p>I'm getting a list, but it's not automagically updating the checkbox states when I refresh those. I suspect a little further reading on my part will resolve this.</p>