列表视图项不';t清除(C#/WPF)
我有一个显示我的项目的列表视图项不';t清除(C#/WPF),c#,.net,wpf,listview,C#,.net,Wpf,Listview,我有一个显示我的项目的列表视图。显示值的过程很好,我没有发现问题,但是当我尝试用新值更新列表时,我需要清理此列表以再次显示新值。我尝试过像lstreults.Items.Clear()这样的方法,但不起作用。有没有合适的方法来进行此操作 Update.cs List<StructLog> All = new List<StructLog>(); if (lstResults.Items.Count > 0) { lstResu
列表视图
。显示值的过程很好,我没有发现问题,但是当我尝试用新值更新列表时,我需要清理此列表以再次显示新值。我尝试过像lstreults.Items.Clear()这样的方法,但不起作用。有没有合适的方法来进行此操作
Update.cs
List<StructLog> All = new List<StructLog>();
if (lstResults.Items.Count > 0)
{
lstResults.DataContext = null;
lstResults.ItemsSource = null;
AllowUIToUpdate();
}
foreach (IPattern p in Patterns.GetAvailablePatterns())
{
p.Start();
Patterns.Results.Add(p.Result());
}
foreach (ObservableCollection<StructLog> res in Patterns.Results)
{
foreach (StructLog r in res)
{
All.Add(r);
}
}
lstResults.ItemsSource = All;
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(All);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("Pattern");
view.GroupDescriptions.Add(groupDescription);
List All=new List();
如果(lstResults.Items.Count>0)
{
lstreults.DataContext=null;
lstResults.ItemsSource=null;
AllowUIToUpdate();
}
foreach(Patterns.GetAvailablePatterns()中的IPattern p)
{
p、 Start();
Patterns.Results.Add(p.Result());
}
foreach(模式中的可观察收集结果)
{
foreach(res中的StructLog r)
{
全部。添加(r);
}
}
lstResults.ItemsSource=全部;
CollectionView视图=(CollectionView)CollectionViewSource.GetDefaultView(全部);
PropertyGroupDescription groupDescription=新的PropertyGroupDescription(“模式”);
view.groupDescription.Add(groupDescription);
main window.xaml
<ListView Name="lstResults" Grid.Row="1" IsEnabled="True" Grid.RowSpan="4" DataContext="All" Grid.ColumnSpan="5" Margin="5,5">
<ListView.View>
<GridView>
<GridViewColumn Header="Linha" Width="Auto" DisplayMemberBinding="{Binding LineNumber}" />
<GridViewColumn Header="Fonte" Width="Auto" DisplayMemberBinding="{Binding Source}" />
<GridViewColumn Header="Data" Width="Auto" DisplayMemberBinding="{Binding Time}" />
<GridViewColumn Header="Log" Width="Auto" DisplayMemberBinding="{Binding LineLog}" />
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Items[0].Pattern, StringFormat={} Teste: {0}}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
<Border CornerRadius="10" Padding="1,1,1,1" BorderThickness="1" BorderBrush="Black" Background="Red" Margin="1,0,0,0" >
<TextBlock Text="{Binding Items.Count,StringFormat={} Items: {0}}" Padding="0" VerticalAlignment="Center" Margin="5,1,5,1" FontSize="15" FontWeight="Bold" Foreground="Black"/>
</Border>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
编辑1
当我为List All=new List()使用Clear
方法时代码>
列表已清除,但仍显示意外的标题,如图1所示
在使用ItemsControl.ItemsSource
时,无法修改ItemsControl.Items
集合。要清理ListView,只需将ItemsSource设置为null,也可以将列表
替换为ObservableCollection
。在那之后,你可以调用observateCollection.Clear
方法。但是使用它我得到了一个异常
什么异常?@mjwills使用lstrests.Items.Clear()之前的lstrests.Items.Clear()
不引发异常,但是仍然不起作用。您是说将ItemsSource属性设置为null并不能清除ListView吗?