WPF ListBox不使用ItemsSource进行更新 [英] WPF ListBox not updating with the ItemsSource
问题描述
以编程方式设置ListBox的ItemsSource:
lstVariable_Selected.ItemsSource = m_VariableList;
列表框的声明如下:
< ListBox Margin =5Name =lstVariable_Selected>
< ListBox.ItemsPanel>
< ItemsPanelTemplate>
< VirtualizingStackPanel Orientation =Horizontal/>
< / ItemsPanelTemplate>
< /ListBox.ItemsPanel>
< ListBox.ItemTemplate>
< DataTemplate>
< Border BorderBrush =GrayBorderThickness =1Margin =0>
< TextBlock FontSize =25Text ={Binding Path = Name}/>
< / Border>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>
当我初始设置ItemsSource时,ListBox(当时不可见)获取其项组。但是,如果我查看列表框,更新似乎停止在那一点。
然后,我可以从m_VariableList集合中删除一个项目,它不会从列表框。同样,如果我添加一个,它不会出现。
什么给了?
您的m_VariableList是否实施 INotifyCollectionChanged ?如果它不是一个ObservableCollection,那么对其内容的更改将不会自动反映在UI中。
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?
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.
这篇关于WPF ListBox不使用ItemsSource进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!