Articles in this section
Category / Section

How to set different row height for each row in .NET MAUI ListView (SfListView)?

3 mins read

The .NET MAUI ListView allows you to customize the row height for each item by utilizing the QueryItemSize event. This event is triggered whenever an item comes into view, with the QueryItemSizeEventArgs argument providing relevant information, including item index, size, and a handled bool.

XAML:

<ContentPage xmlns:sfListView="clr-namespace:Syncfusion.Maui.ListView;assembly=Syncfusion.Maui.ListView">

    <ContentPage.BindingContext>
        <local:ContactsViewModel x:Name="viewModel"/>
    </ContentPage.BindingContext>

    <Grid>
        <sfListView:SfListView    x:Name="listView"  
                                  ItemSpacing="5" 
                                  ItemsSource="{Binding Items}" 
                                  SelectionMode="Multiple">
            <sfListView:SfListView.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="grid">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Label  Grid.Row="0" HorizontalTextAlignment="Center" HorizontalOptions="StartAndExpand" Text="{Binding ContactName}" FontSize="18" />
                        <Label Grid.Row="1" HorizontalTextAlignment="Center" HorizontalOptions="StartAndExpand"  Text="{Binding ContactNumber}" FontSize="15" />
                    </Grid>
                </DataTemplate>
            </sfListView:SfListView.ItemTemplate>
        </sfListView:SfListView>
    </Grid>
</ContentPage>

C# :

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        listView.QueryItemSize += ListView_QueryItemSize;
    }

    private void ListView_QueryItemSize(object sender, QueryItemSizeEventArgs e)
    {
        if (e.ItemIndex % 2 == 0)
            e.ItemSize = 50;
        else
            e.ItemSize = 100;
        e.Handled = true;
    }
}

Output:

QueryItemSize.PNG

Download the complete sample from GitHub.

Conclusion

I hope you enjoyed learning how to set different row heights for each row in .NET MAUI ListView.

You can refer to our .NET MAUI ListView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our .NET MAUI ListView example to understand how to create and manipulate data.

You can check out our components from the License and Downloads page for current customers. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.

Please let us know in the comments section below if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied