【发布时间】:2017-06-20 06:29:23
【问题描述】:
我正在使用 wpf listbox, 在调用重新加载数据函数时无法清除列表,我只想在运行时重新加载新数据,而页面加载时它会正确加载数据,当我刷新新数据时在 itemsource 中获取我可以看到在调试模式下,但列表框中没有新数据,旧数据保留在列表中,我什至无法清除,当我调用 list.items.clear() 时,我尝试了很多方法,有没有我的 XAML 绑定中的问题,以下是我的代码。
XAML:
<ListBox ItemsSource="{Binding}" HorizontalContentAlignment="Left" x:Name="lstbxindex" Foreground="White" FontSize="20px" Height="400" BorderBrush="#555555" Margin="10,34,16,0" VerticalAlignment="Top" Width="322" Background="#555555" >
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel Orientation="Horizontal" Margin="5" >
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="txtblckroundhour" Height="40px" Width="55px" Text="{Binding RoundedHours}" FontSize="14" Background="#555555" Loaded="txtblckroundhour_Loaded" Foreground="White"></TextBlock>
<Label x:Name="items" MouseDoubleClick="items_MouseDoubleClick" Content="{Binding ProjectRow.Name}" Background="#555555" FontSize="20" Loaded="items_Loaded" Visibility="Visible" Margin="35,0,0,0" Width="230" Foreground="White"></Label>
</StackPanel>
<StackPanel Orientation="Vertical">
<ComboBox Height="40px" Width="290" Margin="-230,0,0,0" Loaded="ComboBox_Loaded" Visibility="Hidden" IsEditable="True" FontSize="20" Background="White" Foreground="Black"></ComboBox>
</StackPanel>
<!--<ComboBox x:Name="ComboBox_AddItem" Height="40px" Width="290" Margin="-35,35,0,0" Loaded="ComboBox_AddItem_Loaded" IsEditable="True" FontSize="20" Background="White" Visibility="Hidden" Foreground="Black"></ComboBox>-->
</WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
获取值列表
private List<ProjectInformation> projectInformationList1 = new List<ProjectInformation>();
// 这里定义用户界面列表框到内存中对象列表的实际绑定。
foreach (DtoProjectsRow row in projectsTable.Rows)
{
projectInformationList1.Add(new ProjectInformation(row));
}
lstbxindex.DataContext = projectInformationList1;
在 SO 我尝试了一些解决方案,但不幸的是它不适合我。上次我试过了,
XAML.cs 页面
public static readonly DependencyProperty MyListProperty = DependencyProperty.Register("MyList", typeof(ObservableCollection<String>), typeof(Window));
public ObservableCollection<String> MyList
{
get
{
return (ObservableCollection<String>)GetValue(MyListProperty);
}
set
{
SetValue(MyListProperty, value);
}
}
XAML:
<ListBox ItemsSource="{Binding **ElementName=Window**}" HorizontalContentAlignment="Left" x:Name="lstbxindex" Foreground="White" FontSize="20px" Height="400" BorderBrush="#555555" Margin="10,34,16,0" VerticalAlignment="Top" Width="322" Background="#555555" >
使用上面的解决方案列表项很清晰,但是当页面加载列表框项很清晰但我不想清除 iistboxitems,在从用户更新值后它将重新加载列表框中的更新值。
lstbxindex.ItemsSource = null;
但它不起作用。对于加载所有项目的页面加载列表框,每 15 分钟间隔它会调用加载函数,第一次它会重新加载更新的值,但第二次它会重新加载更新的值,并且以前的值再次保留在列表框中。
【问题讨论】:
-
抛出了什么异常?
-
你遇到了什么异常?
-
没有抛出异常。它只是在列表框中再次重新加载整个数据(更新值)。
-
有几件事,先别做
lstbxindex.ItemsSource = null;。如果您有 ViewModel,请不要使用后面的代码,因为它会让人感到困惑。你声明private List<ProjectInformation> projectInformationList1 = new List<ProjectInformation>();,但在你的foreach中你使用projectInformationList.Add(new ProjectInformation(row));,这是一个错字吗?最后,尝试像ItemsSource="{Binding projectInformationList1, Mode=TwoWay}"这样的双向绑定 -
@keyurPATEL 但它会在页面加载时清除 listboxitem。
标签: c# wpf xaml listbox textblock