We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to format the group header for different levels of grouping in Xamarin.Forms ListView(SfListView)?


You can format the different levels of the group by binding the converter for the loaded elements to the GroupHeaderTemplate in Xamarin.Forms SfListView.

Xaml

Defined group header template with converter.

<syncfusion:SfListView.GroupHeaderTemplate>
    <DataTemplate>
        <ViewCell>
            <ViewCell.View>
                <StackLayout BackgroundColor="{Binding Level, Converter={StaticResource groupHeaderConverter}}" 
                                Padding="{Binding Level,Converter={StaticResource groupHeaderConverter}}">
                    <Label x:Name="label" Text="{Binding Key}" 
                            FontSize="22"                                   
                            FontAttributes="Bold"                                    
                            VerticalOptions="Center" 
                            HorizontalOptions="Start" 
                            />
                </StackLayout>
            </ViewCell.View>
        </ViewCell>
    </DataTemplate>
</syncfusion:SfListView.GroupHeaderTemplate>

C#

Set BackgroundColor and Padding to the different group levels.

public class GroupHeaderConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (targetType.Name == "Color")
        {
            if ((int)value == 1)
                return Color.Aqua;
            else
                return Color.AntiqueWhite;
        }
        else
        {
            if ((int)value == 1)
                return new Thickness(5, 5, 5, 0);
            else
                return new Thickness((int)value * 15, 5, 5, 0);
        }
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

Output:

Image for customizing group header based on level in Xamarin.Forms SfListView.

View sample in GitHub

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile