Access to items of previous group in group header

Hello,


i know that i can use a converter to aggregate some data of the items in a group header, like described in the documentation https://help.syncfusion.com/xamarin/listview/grouping?cs-save-lang=1&cs-lang=csharp#aggregate-summary

But is it possible to also access the items of the previous (or following) group in the group header if i use a converter?


Regards,

  Kalle


1 Reply 1 reply marked as answer

LN Lakshmi Natarajan Syncfusion Team December 7, 2021 01:38 PM UTC

Hi Kalle, 
 
Thank you for using Syncfusion support. 
 
We would like to inform you that you can get the previous and next group items from the ListView.DataSource.Groups list.  
 
Please refer to the following code snippets for more reference, 
 
Converter: 
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
{ 
    int result = 0; 
    var currentGroup = value as GroupResult; 
 
    if (currentGroup != null) 
    { 
        var listView = parameter as SfListView; 
 
        var currentGroupIndex = listView.DataSource.Groups.IndexOf(currentGroup); 
                 
        //Current group items 
        var items = currentGroup.Items as IEnumerable; 
 
        if (currentGroupIndex != 0) 
        { 
            var previousGroup = listView.DataSource.Groups[currentGroupIndex - 1]; 
            //You can get the previous group items as previousGroup.Items 
        } 
 
        if (currentGroupIndex < listView.DataSource.Groups.Count - 1) 
        { 
            var nextGroup = listView.DataSource.Groups[currentGroupIndex + 1]; 
            //You can get the next group items as nextGroup.Items 
        } 
 
        if (items != null) 
        { 
            var groupitems = items.ToList<object>().ToList<object>(); 
 
            if (groupitems != null) 
            { 
                for (int i = 0; i < groupitems.Count; i++) 
                { 
                    var contact = groupitems[i] as Contacts; 
                    string temp = null; 
                    foreach (var charac in contact.ContactNumber) 
                    { 
                        if (charac != ',') 
                            temp += charac; 
                    } 
                    var dd = System.Convert.ToInt32(temp); 
                    result += dd; 
                } 
            } 
        } 
    } 
    return result; 
} 
 
XAML: Pass the SfListView instance as parameter to the converter. 
<listView:SfListView.GroupHeaderTemplate> 
    <DataTemplate x:Name="GroupHeaderTemplate" > 
        <ViewCell> 
            <ViewCell.View> 
                <Grid BackgroundColor="#E4E4E4"> 
                    <Grid.ColumnDefinitions> 
                        <ColumnDefinition Width="*" /> 
                        <ColumnDefinition Width="Auto" /> 
                        <ColumnDefinition Width="Auto" /> 
 
                    </Grid.ColumnDefinitions> 
                    <Label Text="{Binding Key}" 
        FontSize="18" 
        FontAttributes="Bold" 
        VerticalOptions="Center" 
        HorizontalOptions="Start" 
        Margin="20,0,0,0" /> 
                    <Label Text="{Binding .,Converter={StaticResource Converter}, 
                        ConverterParameter={x:Reference listView}}" Grid.Column="2" 
        FontSize="18" 
        FontAttributes="Bold" 
        VerticalOptions="Center" 
        HorizontalOptions="Start" 
        Margin="0,0,0,0" /> 
 
                    <Label Text="Sum of salary: " Grid.Column="1" FontSize="18" FontAttributes="Bold"  HorizontalOptions="End" VerticalOptions="Center"/> 
                </Grid> 
            </ViewCell.View> 
        </ViewCell> 
    </DataTemplate> 
</listView:SfListView.GroupHeaderTemplate> 
 
 
Please let us know if you need further assistance. 
 
Lakshmi Natarajan 
 


Marked as answer
Loader.
Up arrow icon