How to set different row height for each row in .NET MAUI ListView (SfListView)?
The .NET MAUI SfListView allows you to customize the size of each row by utilizing the QueryItemSize event based on the item index. During this event, the Handled property indicates whether to set a specified size for an item or not, determined by its Boolean value. This event is triggered when an item comes into view, with the QueryItemSizeEventArgs argument providing relevant information.
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:
Conclusion
I hope you enjoyed learning how to set different row height 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 for configuration specifications. You can also explore our .NET MAUI ListView example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!