【问题标题】:Grouping ListView under PivotItem in Windows 10 UWP在 Windows 10 UWP 中的 PivotItem 下对 ListView 进行分组
【发布时间】:2017-10-19 01:52:36
【问题描述】:

在我的情况下,我目前正在根据 billing_id 对 Listview 进行分组。我可以使用 LINQ 的 GroupBy 按项目检索分组,但是在显示它时遇到了问题。它显示为整个页面,而不是 PivotItem 的子项。我的代码如下。

首先我在页面资源中创建了一个CollectionViewSource如下

<Page.Resources>
    <CollectionViewSource x:Key="cvs" x:Name="cvs" IsSourceGrouped="True" />
</Page.Resources>

然后我使用 GroupBy 检索所有列表并将其分配给此 CollectionViewSource,如下所示

var billingGroupByList = taskBillingList.GroupBy(b => b.billing_type_id).Select(grp => grp.ToList()).ToList();
this.cvs.Source = billingGroupByList;

之后,我创建了包含分组样式和 ItemTemplate 的 UI,如下所示

<Pivot>
  <Pivot.Items>
     <PivotItem x:Name="ChargesPivot"
               Header="Charges"
               Background="White"
               Margin="0,-35,0,0">
          <ScrollViewer>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <StackPanel>
                    <Grid Background="#F8F8FB" BorderBrush="#D6D6D6" BorderThickness="5,0,0,0">
                        <TextBlock x:Name="charges"
                                   Text="Charges"
                                   FontSize="18"
                                   Margin="10"
                                   Foreground="Gray"/>
                    </Grid>

                    <ListView x:Name="chargesView"
                              ItemContainerStyle="{StaticResource GenericListViewContainerStyle}"
                              ItemsSource="{Binding Source={StaticResource cvs}}"                          SelectionChanged="chargesView_SelectionChanged"
                              Margin="5">
                        <ListView.GroupStyle>
                            <GroupStyle>
                                <GroupStyle.HeaderTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding billing_type}"
                                                   FontSize="16"
                                                   FontWeight="SemiBold"/>
                                    </DataTemplate>
                                </GroupStyle.HeaderTemplate>
                            </GroupStyle>
                        </ListView.GroupStyle>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>

                                <Grid Grid.Row="0">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width=".25*"/>
                                        <ColumnDefinition Width=".25*"/>
                                        <ColumnDefinition Width=".25*"/>
                                        <ColumnDefinition Width=".25*"/>
                                    </Grid.ColumnDefinitions>

                                    <TextBlock x:Name="billing_date"
                                               FontSize="16"
                                               Margin="4,1,4,4"
                                               Grid.Column="0"
                                               FontWeight="SemiBold"
                                               Text="{Binding Path = quantity}"/>
                                    <TextBlock x:Name="charge"
                                               FontSize="16"
                                               Margin="4,1,4,4"
                                               Grid.Column="1">
                                        <Run Text="{Binding Path = charge}"/>
                                        <Run Text=" "/>
                                        <Run Text="{Binding Path = charge_uom}"/>
                                    </TextBlock>
                                </Grid>
                            </Grid>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackPanel>
        </Grid>
    </ScrollViewer>
</PivotItem>

有人可以建议,我做错了什么吗?这只是将 Charge 显示为页面标题,也使我的其他页面 UI 隐藏。我只想将其显示为页面的一部分。

我想实现类似下面的截图

【问题讨论】:

  • 枢轴项目在哪里?
  • &lt;PivotItem&gt;&lt;/PivotItem&gt;这个。
  • 您尚未发布该代码。请发布完整代码
  • @Archana 发布了整个代码。输出屏幕截图只是我想要实现的示例屏幕截图。
  • 好像你错过了 LIstView 的绑定 ItemSource。 ItemsSource="{Binding Source={StaticResource cvs}}"

标签: c# listview uwp grouping windows-10-universal


【解决方案1】:

问题是,当您的项目被分组时,该组没有可显示的内容。如果您查看您的分组:

var billingGroupByList = taskBillingList.GroupBy(b => b.billing_type_id)
    .Select(grp => grp.ToList()).ToList();

如果您将鼠标悬停在var 上,您会看到billingGroupByList 的类型是List&lt;List&lt;BillingItem&gt;&gt;。要显示“组标题”,您需要一个具有要显示的属性的对象。删除 linq 的 Select 部分,您的对象将是 List&lt;IGrouping&lt;int, BillingItem&gt;&gt;。 IGrouping 有一个Key 属性,然后您可以在您的组样式中显示该属性。

<ListView.GroupStyle>
    <GroupStyle>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Key}"/>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>
</ListView.GroupStyle>

【讨论】:

  • 谢谢。这很好用,我在 BillingItem 对象中有billing_type,如何在 GroupStyle 中使用它?我尝试绑定它,但它显示为空。我只能在HeaderTemplate 中使用IGrouping Key 吗?
  • 您是按 billing_id 还是 billing_type 分组?你可以按 billing_type 分组吗?
  • 是的。我能做到。
  • 如果你可以按类型分组,那么 IGrouping 的 Key 就是你的 billing_type 属性
  • 谢谢,现在可以使用了。我走的是正确的道路,只是在使用List&lt;List&lt;Billing&gt;&gt; 而不是IGrouping 时犯了一个小错误。非常感谢您的快速帮助。只是一个简单的问题,我可以删除灰色分隔线(灰线)吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
  • 2017-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多