加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Wpf ListView是否可以以不同于组头的方式订购组项?

发布时间:2020-12-15 17:45:40 所属栏目:百科 来源:网络整理
导读:我有一个列表视图控件与分组和排序. 组标题是按降序排列的日期. 我试图找出如何按升序排序每个组头下的分组项目,但无法弄清楚如何完成它或者甚至可以使用ListView. 这是我到目前为止的XAML. 注意:ScheduledItemSearchResults是一个可观察的ScheduleItem集合
我有一个列表视图控件与分组和排序.

组标题是按降序排列的日期.

我试图找出如何按升序排序每个组头下的分组项目,但无法弄清楚如何完成它或者甚至可以使用ListView.

这是我到目前为止的XAML.

注意:ScheduledItemSearchResults是一个可观察的ScheduleItem集合,每个项目都有Title和ScheduleDate属性.

<Grid x:Name="TxScheduleItemResults"
              Grid.Column="1">
            <Grid.Resources>
                <CollectionViewSource Source="{Binding ScheduledItemSearchResults}" x:Key="scheduledItems">
                    <CollectionViewSource.SortDescriptions>
                        <scm:SortDescription PropertyName="Value.ScheduleDateTime" Direction="Descending"/>
                    </CollectionViewSource.SortDescriptions>
                    <CollectionViewSource.GroupDescriptions>
                        <dat:PropertyGroupDescription PropertyName="Value.ScheduleDateTime.Date" />
                    </CollectionViewSource.GroupDescriptions>
                </CollectionViewSource>
            </Grid.Resources>

            <ListView x:Name="ScheduledItemResultsList"
                      Style="{StaticResource TransparentListViewStyle}"
                      ItemContainerStyle="{StaticResource alternatingListViewItemStyle}" 
                      AlternationCount="2"
                      ItemsSource="{Binding Source={StaticResource scheduledItems}}"
                      >

                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Scheduled Items"
                                        Width="{Binding ElementName=ScheduledItemResultsList,Path=ActualWidth}"
                                        >
                            <GridViewColumn.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock Style="{StaticResource ModuleGroupHeader}"
                                               Text="{Binding}"
                                               />
                                </DataTemplate>
                            </GridViewColumn.HeaderTemplate>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBox Text="{Binding Value.Title}" Width="200"/>
                                        <TextBox Text="{Binding Value.ScheduleDateTime,StringFormat={}{0:HH:mm:ss}}" Width="120"/>
                                    </StackPanel>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate>
                                            <Expander IsExpanded="True">
                                                <Expander.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock Text="{Binding  Path=Items[0].Value.ScheduleDateTime.Date,StringFormat={}{0:dd/MM/yyyy}}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                                        <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0" VerticalAlignment="Bottom" />
                                                        <TextBlock Text=" item(s)" FontSize="22" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />
                                                    </StackPanel>
                                                </Expander.Header>
                                                <ItemsPresenter />
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </ListView.GroupStyle>
            </ListView>
        </Grid>

解决方法

您可以在一个CollectionViewSource中包含多个SortDescriptions元素:
<CollectionViewSource Source="{Binding ScheduledItemSearchResults}" x:Key="scheduledItems">
                <CollectionViewSource.SortDescriptions>
                    <!--This will sort groups-->
                    <scm:SortDescription PropertyName="Value.ScheduleDateTime.Date" />
                    <!--This will items-->
                    <scm:SortDescription PropertyName="Value.ScheduleDateTime" Direction="Descending"/>
                </CollectionViewSource.SortDescriptions>
                <CollectionViewSource.GroupDescriptions>
                    <dat:PropertyGroupDescription PropertyName="Value.ScheduleDateTime.Date" />
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>

附:我不知道你究竟想要对它进行排序,但是如果你对第一组进行排序,然后对项目进行排序则应该有效.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读