Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
793 views
in Technique[技术] by (71.8m points)

c# - WPF ListBox not updating with the ItemsSource

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...