Articles in this section
Category / Section

How to add a separator between items in .NET MAUI ListView (SfListView) ?

4 mins read

You can add a separator between ListViewItems in .NET MAUI ListView (SfListView) by utilizing a BoxView to represent the separator line.

XAML

In the SfListView.ItemTemplate, a BoxView with HeightRequest set to 1 is added to show the separator line. Bind the converter to the IsVisible property to manage the visibility of the separator line of the last item.

<ContentPage x:Class="ListViewMaui.MainPage"
             xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ListViewMaui"
             xmlns:listView="clr-namespace:Syncfusion.Maui.ListView;assembly=Syncfusion.Maui.ListView"   
             BackgroundColor="White">
 
    <ContentPage.Resources>
        <ResourceDictionary>
            <local:SeparatorVisibilityConverter x:Key="separatorVisibilityConverter"/>
        </ResourceDictionary>
    </ContentPage.Resources>
 
    <listView:SfListView x:Name="listView" ItemsSource="{Binding BookInfo}" ItemSize="120">
        <listView:SfListView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <StackLayout Margin="10,0,0,0" VerticalOptions="StartAndExpand">
                        <Label Text="{Binding BookName}" FontSize="20" FontAttributes="Bold" VerticalOptions="CenterAndExpand"/>
                        <Label Text="{Binding BookDescription}" FontSize="15" VerticalOptions="StartAndExpand"/>
                    </StackLayout>
                    <BoxView HeightRequest="1" BackgroundColor="LightGray"
                             IsVisible="{Binding .,Converter={StaticResource separatorVisibilityConverter}, ConverterParameter={x:Reference Name=listView}}"/>
                </StackLayout>
            </DataTemplate>
        </listView:SfListView.ItemTemplate>
    </listView:SfListView>
</ContentPage>

C#

The following converter returns false for the last item that can be accessed from the DataSource.DisplayItems, and true for other items.

public class SeparatorVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var listView = parameter as SfListView;
 
        if (value == null)
            return false;
 
        return listView.DataSource.DisplayItems[listView.DataSource.DisplayItems.Count - 1] != value;
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Output

Separator line between item in .NET MAUI SfListView


Download the complete sample on GitHub.

Conclusion

I hope you enjoyed learning how to add a separator between items in .NET MAUI ListView (SfListView).

You can refer to our .NET MAUI ListView feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. Explore our .NET MAUI ListView example to understand how to create and manipulate data. For current customers, check out our components from the License and Downloads page. 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 if you have any queries or require clarification. 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