如何对 WPF ListView 中的项目进行分组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/639809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:08:02  来源:igfitidea点击:

How do I group items in a WPF ListView

wpf.net-3.5styles

提问by Russ

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

我有一个ListView我想将结果分组的,但是我发现的例子不起作用。如何对结果进行分组?

I want to group on the Statusproperty of a custom object.

我想对Status自定义对象的属性进行分组。

This is what I have:

这就是我所拥有的:

<ListView IsSynchronizedWithCurrentItem="True"
          ItemsSource="{Binding}"
          HorizontalContentAlignment="Stretch"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled"
          Background="Transparent" SelectionChanged="ListView_SelectionChanged"
          Name="lstShelvedOrders">

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

        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="BorderThickness" Value="1" />
                <Setter Property="Width" Value="Auto" />
                <Setter Property="FontSize" Value="10.4"  />               
            </Style>
        </ListView.ItemContainerStyle>

        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Path=Number}" Header="Shelve ID"  />
                <GridViewColumn DisplayMemberBinding="{Binding Path=Customer}" Header="Customer" />
                <GridViewColumn DisplayMemberBinding="{Binding Path=PurchaseOrderNo}" Header="PO Number" />
                <GridViewColumn DisplayMemberBinding="{Binding Path=SubmittedBy}" Header="Shelved By"  />
                <GridViewColumn DisplayMemberBinding="{Binding Path=OrderDate, StringFormat=MMM dd\, yyyy}" Header="Date"  />
                <GridViewColumn DisplayMemberBinding="{Binding Path=CustomerTerms.Description}" Header="Order Terms"  />
                <GridViewColumn DisplayMemberBinding="{Binding Path=ShippingMethod.Description}" Header="Shipping"  />
                <GridViewColumn DisplayMemberBinding="{Binding Path=TotalPrice, StringFormat=c}" Header="Order Total"  />
            </GridView>
        </ListView.View>
    </ListView>

And this is the code that I have:

这是我拥有的代码:

 void ShelvedOrderList_DataContextChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
 {
     AddGrouping();
 }

 private void AddGrouping()
 {
     if ( lstShelvedOrders.ItemsSource == null)
     {
         return;
     }

     CollectionView myView = (CollectionView)CollectionViewSource.GetDefaultView(lstShelvedOrders.ItemsSource);
     PropertyGroupDescription groupDescription = new PropertyGroupDescription("Status");
     myView.GroupDescriptions.Add(groupDescription);
 }

采纳答案by Robert Macnee

I notice one thing right away - the GroupStyle.HeaderTemplatewill be applied to a CollectionViewGroup,so your DataTemplateshould probably look like this:

我马上注意到一件事 -GroupStyle.HeaderTemplate将应用于 a CollectionViewGroup所以你DataTemplate应该看起来像这样:

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

CollectionViewGroup.Namewill be assigned the value of Statusfor that group.

CollectionViewGroup.NameStatus为该组分配 的值。

回答by MAXE

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

我认为这也可以更好,使用带有新 ControlTemplate 的 GroupStyle:

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