ListView项目不清除(C#/ WPF)

时间:2017-11-23 12:00:21

标签: c# .net wpf listview

我有.... for (var i = 0; i < txt.length; i++) { var item = d.data.Frequency[0].Title; console.log(item); } 来展示我的物品。显示值的过程非常正常,我没有发现问题,但是当我尝试使用新值更新列表时,我需要清理此列表以再次显示新值。我尝试过像listView这样的方法不起作用。有一种正确的方法可以进行此操作吗?

Update.cs

lstResults.Items.Clear()

MainWindow.xaml

    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);

编辑1

当我使用<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> 方法Clear时 列表清理,但它重新标记了一个意外的标题,显示在图像1

Error using clear

1 个答案:

答案 0 :(得分:2)

使用ItemsControl.Items时,您无法修改ItemsControl.ItemsSource集合。要清理ListView,只需将ItemsSource设置为null,或者可以将List<StructLog>替换为ObservableCollection<StructLog>。之后,您可以调用ObservableCollection.Clear方法。