How do I group items in a WPF ListView

后端 未结 2 2035
挽巷
挽巷 2020-11-30 06:42

I have a ListView that I want to group results into, however the examples I am finding are not working. How can I group my results?

I want to group on t

相关标签:
2条回答
  • 2020-11-30 07:02

    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.

    0 讨论(0)
  • 2020-11-30 07:11

    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>
    
    0 讨论(0)
提交回复
热议问题