Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wpf ListView Is it possible to order the group items differently from the group headers?

Tags:

c#

listview

wpf

I have a List View Control with grouping and sorting.

The group headers are Dates in descending order.

I am trying to find out how to order the grouped items under each group header in an ascending order, but cant figure out how to get it done or if it is even possible with ListView.

Here is the XAML I have so far.

Note: ScheduledItemSearchResults is an observable collection of ScheduleItems each item has a Title and a ScheduleDate property.

<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>
like image 301
John Avatar asked Feb 06 '15 20:02

John


1 Answers

You can have several SortDescriptions elements in one CollectionViewSource:

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

P.S. I don't quite get how exactly you want to sort it, but if you sort first groups and then items it should work.

like image 120
Nadia Chibrikova Avatar answered Nov 10 '22 20:11

Nadia Chibrikova