I've got a simple ListView which pulls data from an ObservableCollection and I'm using a CollectionViewSource to a set a GroupPropertyDescription so that my rows appear grouped. I've used the following style to use an expander for each group:
<Style TargetType="{x:Type GroupItem}" x:Key="listViewGroupStyle">
<Setter Property="Margin" Value="0 0 0 5" />
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="False" BorderBrush="{Binding Name, Converter={StaticResource statusForegroundConverter}}" BorderThickness="2" Margin="1">
<Expander.Header>
<Border BorderThickness="3" BorderBrush="{Binding Name, Converter={StaticResource statusBackgroundConverter}}" CornerRadius="6">
<DockPanel TextBlock.FontWeight="Bold" TextBlock.Foreground="{Binding Name, Converter={StaticResource statusForegroundConverter}}">
<TextBlock Text="{Binding Name, Converter={StaticResource statusStringConverter}}" Margin="5 2 2 2" />
<TextBlock Text=" - " Margin="2" />
<TextBlock Text="{Binding Path=ItemCount}" Margin="0 2 5 2"/>
</DockPanel>
</Border>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The problem I have is that my collection is updated frequently during runtime and everytime it is, all the groups get collapsed regardless of whether or not I had some open.
Is there a way for me to maintain the appearance of the ListView after my collection has been reassigned?
I've found a solution, with most of the details here:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/f022425b-f685-4a7a-91df-828103871ebc
The main point is that when an ItemsSource is reassigned, it invalidates the associated control, causing it to redraw. However, as I was using an ObservableCollection, it's far easier to accumulate your new data in a temporary list and add or remove objects from the original list as necessary:
ObservableCollection<xxx> tempBuilds = new ObservableCollection<xxx>();
IEnumerable<xxx> addRange = tempBuilds.Except(_filteredBuilds);
IEnumerable<xxx> removeRange = _filteredBuilds.Except(tempBuilds);
addRange.ToList().ForEach(bm => _filteredBuilds.Add(bm));
removeRange.ToList().ForEach(bm => _filteredBuilds.Remove(bm));