10

I have what I believe should be simple two-way databinding in WPF setup, but the listbox (target) is not updating as the collection changes.

I'm setting this ItemsSource of the ListBox programmatically:

lstVariable_Selected.ItemsSource = m_VariableList;

And the ListBox is declared as follows:

<ListBox Margin="5" Name="lstVariable_Selected">
    <ListBox.ItemsPanel>
       <ItemsPanelTemplate>
          <VirtualizingStackPanel Orientation="Horizontal"/>
       </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
       <DataTemplate>
            <Border BorderBrush="Gray" BorderThickness="1" Margin="0">
                <TextBlock FontSize="25" Text="{Binding Path=Name}" />
            </Border>
       </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

When I initially set the ItemsSource, the ListBox (which is not visible at the time) gets its items set. However, if I go view the ListBox, updates seem to stop at that point.

I can then remove an item from the m_VariableList collection, and it does not disappear from the ListBox. Likewise, if I add one, it doesn't appear.

What gives?

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Paul Prewett
  • 2,003
  • 3
  • 23
  • 35

3 Answers3

23

Is your m_VariableList implementing INotifyCollectionChanged? If it's not an ObservableCollection, then changes to it's contents will not automatically be reflected in the UI.

rmoore
  • 15,162
  • 4
  • 59
  • 59
  • It is a BindingList, which according to Rocky (http://forums.lhotka.net/forums/thread/17402.aspx) works with both WPF and WinForms, whereas INotifyCollectionChanged is WPF-only. – Paul Prewett Jun 16 '09 at 20:59
  • If it implements IBindingList then it will work just the same. There's something else going on that's not apparent from what you've described as that will work. – rmoore Jun 16 '09 at 21:41
  • Turns out that the underlying implementation had changed (unbeknownst to me) and it was no longer implementing IBindingList when i thought that it was. Moral: Check your (my) assumptions. – Paul Prewett Jun 22 '09 at 17:55
8

The problem is not in the XAML that you have provided. I used the same XAML successfully in a test application; however, I was able to replicate the issue you are experiencing by re-instantiating the m_VariableList variable.

When the m_VariableList is given a new instance, or pointed to a new object, it is not reflected in the ListBox because the control has its own reference to the data. This may not be the cause of your problem, but I'd recommend looking over your code-behind to ensure that the variable is not getting re-instantiated.

Tony Borres
  • 1,546
  • 11
  • 11
  • Thanks, Tony. I went spelunking to try and figure out where the collection may have been getting re-instantiated when I found that the base class had changed. – Paul Prewett Jun 22 '09 at 17:56
4

i got stuck for more than hour and then simple logic solved this problem just set itemsource to clear list and then set source u need again

lstVariable_Selected.ItemsSource = new List<Object>();
lstVariable_Selected.ItemsSource = m_VariableList;