我正在使用WPF列表框,但在调用reload data函数时无法清除列表,我只想在运行时重新加载新数据,当页面加载时,它能够正确加载数据,当我刷新时,新数据是从itemsource中获取的,我可以在调试模式下看到,但列表框中没有新数据,旧数据仍然存在于列表中,我甚至无法清除,当我调用list.items.clear()时,它会抛出异常并导致应用程序崩溃,我尝试了很多方法,是否我的XAML绑定存在问题?以下是我的代码:
<DataTemplate x:Key="listBoxTemplate">
<StackPanel Margin="3">
<DockPanel >
<TextBlock FontWeight="Bold" Text="{Binding Name}" DockPanel.Dock="Left" Margin="5,0,10,0"/>
<TextBlock Text=" " />
<TextBlock Text="{Binding Percnt}" Foreground="Green" FontWeight="Bold" />
</DockPanel>
</StackPanel>
</DataTemplate>
我的列表框
<ListBox Height="898" Name="lstEntity" Width="291" ItemTemplate="{StaticResource listBoxTemplate}" SelectionChanged="lstEntity_SelectionChanged"/>
绑定代码
lstEntity.ItemsSource = sei.getNames();
getNames()函数只是返回数据列表,没有什么特殊的代码。如何解决这个问题。