Juliano Oliveira
Juliano Oliveira

Reputation: 928

ListView Items don't clear (C#/WPF)

I have a listView that show my items. The process to show the values work very fine and i don't find a problem, but when I try to update the list with new values, I need to clean this list to show new values again. I have tried methods like lstResults.Items.Clear() don't work. There is a properly way to make this operation?

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

MainWindow.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>

EDIT 1

When I use the method Clear for List<StructLog> All = new List<StructLog>(); The list clean, but it remaing a header unexpected, show in Image 1

Error using clear

Upvotes: 2

Views: 867

Answers (1)

Alexis
Alexis

Reputation: 825

You cannot modify ItemsControl.Items collection when using the ItemsControl.ItemsSource. To clean the ListView you can simply set ItemsSource to null, or you can replace the List<StructLog> with the ObservableCollection<StructLog>. After that, you can call the ObservableCollection.Clear method.

Upvotes: 3

Related Questions