How do I group items in a WPF ListView

Solution 1:

I notice one thing right away - the GroupStyle.HeaderTemplate will be applied to a CollectionViewGroup, so your DataTemplate should probably look like this:

<GroupStyle>
    <GroupStyle.HeaderTemplate>
        <DataTemplate>
            <TextBlock FontSize="15" FontWeight="Bold" Text="{Binding Name}"/>
        </DataTemplate>
    </GroupStyle.HeaderTemplate>
</GroupStyle>

CollectionViewGroup.Name will be assigned the value of Status for that group.

Solution 2:

I think this can also be better, using a GroupStyle with a new ControlTemplate:

<ListView ItemsSource="{Binding Path=ContactsView}">
  <ListView.GroupStyle>
    <GroupStyle>
      <GroupStyle.ContainerStyle>
        <Style TargetType="{x:Type GroupItem}">
          <Setter Property="Template" Value="{StaticResource ContactsGroupItemTemplate}" />
        </Style>
      </GroupStyle.ContainerStyle>
    </GroupStyle>
  </ListView.GroupStyle>

...

<ControlTemplate TargetType="{x:Type GroupItem}" x:Key="ContactsGroupItemTemplate">
  <Expander IsExpanded="False">
    <Expander.Header>
      <DockPanel>
        <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100"/>
          <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
          <TextBlock FontWeight="Bold" Text=" Items"/>
      </DockPanel>
    </Expander.Header>
    <Expander.Content>
      <ItemsPresenter />
    </Expander.Content>
  </Expander>
</ControlTemplate>