c# - Wpf ListView 是否可以以不同于组标题的方式对组项目进行排序?

标签 c# wpf listview

我有一个带分组和排序功能的 ListView 控件。

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

我试图找出如何按升序对每个组标题下的分组项目进行排序,但无法弄清楚如何完成它,或者是否可以使用 ListView。

这是我目前拥有的 XAML。

注意:ScheduledItemSearchResults 是一个可观察的 ScheduleItems 集合,每个项目都有一个 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,0,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>

附言我不太明白您想如何对其进行排序,但如果您首先对组进行排序,然后对项目进行排序,它应该可以工作。

关于c# - Wpf ListView 是否可以以不同于组标题的方式对组项目进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28374641/

相关文章:

c# - ASMX 网络服务测试工具

c# - 如何在 WPF 中找到 UserControl 宽度?

c# - 使用 PDFSharp 将图像叠加到 PDF 上

c# - Mono Evaluator类

c# - 如何在 GroupBox 标题中包装文本?

c# - 如何在 C# 中调用外部 exe 时使 UI 响应?

android - ListView 上下文菜单与列表子项可聚焦的 android

android - 从 ListView 适配器单击按钮时显示的上下文菜单

Android:如何在应用过滤器后找出适配器中项目的*原始*位置

c# - 调用 GC.SuppressFinalize(this) 时是否存在不使用 "this"的用例?