Grouping with Key Selector

Hello,

I am having a problem with grouping. The listview is splitting up every single item and not grouping the like ones together. It works if I get rid of the KeySelector, but then I cannot get my group headings to show to both the icon and text. My code is below:

Code Behind:
            todayListView.DataSource.GroupDescriptors.Add(new GroupDescriptor()
            {
                PropertyName = "PartOfTheDay",
                KeySelector = (object obj1) =>
                {
                    var temp = (obj1 as item);
                    return temp;
                },
            });

        <syncfusion:SfListView 
            AbsoluteLayout.LayoutBounds="0.0, 0.0, 1.0, 1.0"
                     AbsoluteLayout.LayoutFlags="All"
                     x:Name="todayListView" Style="{StaticResource ListViewStyle}"
                     VerticalOptions="FillAndExpand"
                     SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding Items}"
                     ItemTemplate="{StaticResource dataTemplate}"
                        AutoFitMode="DynamicHeight">
            <syncfusion:SfListView.GroupHeaderTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <StackLayout BackgroundColor="{StaticResource SecondaryBackgroundColor}">
                                <StackLayout Orientation="Horizontal" VerticalOptions="CenterAndExpand" Padding="10,10,10,5">
                                    <app:CustomImage Source="{Binding Key.HeaderIcon}" TintColor="{Binding Key.HeaderColor}"  VerticalOptions="Center"                                              HeightRequest="20" />
                                    <Label Text="{Binding Key.HeaderText}" Style="{StaticResource SecondaryCaptionStyle}" VerticalOptions="Center" TextColor="{Binding                                         Key.HeaderColor}" />
                                </StackLayout>
                                <BoxView Style="{StaticResource BoxviewSeperator}" />
                            </StackLayout>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </syncfusion:SfListView.GroupHeaderTemplate>

1 Reply 1 reply marked as answer

LN Lakshmi Natarajan Syncfusion Team January 14, 2021 08:13 AM UTC

Hi Spencer, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query “Grouping with Key Selector” from our side. We would like to inform you that the KeySelector is used to return the Group key and the Group key should be the string value. But, in the code snippets, you are returning the object as the KeySelector which will group every item as group. This is the grouping behavior of the SfListView.  
 
You can achieve your requirement using converters to bind model data to the group header. Please refer to our online documentation regarding the requirement from the following link, 
KB links: 
 
Please let us know if this helps. 
 
Regards, 
Lakshmi Natarajan 


Marked as answer
Loader.
Up arrow icon