How to work with Accordion with SfListView in Xamarin.Forms (SfAccordion)?
You can add SfListView as the Content of SfAccordion in Xamarin.Forms Accordion.
XAML
Add SfListView to the SfAccordion.Content. Define the converter for HeightRequest to update the height of the SfListView based on the items.
<ContentPage xmlns="h
ttp://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:AccordionXamarin" xmlns:syncfusion="clr-namespace:Syncfusion.XForms.Accordion;assembly=Syncfusion.Expander.XForms" xmlns:sfListView="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms" xmlns:converter="clr-namespace:AccordionXamarin.Converter" x:Class="AccordionXamarin.MainPage" Padding="{OnPlatform iOS='0,40,0,0'}"> <ContentPage.BindingContext> <local:ItemInfoRepository/> </ContentPage.BindingContext> <ContentPage.Resources> <ResourceDictionary> <converter:HeightConverter x:Key="HeightConverter"/> </ResourceDictionary> </ContentPage.Resources> <ContentPage.Content> <syncfusion:SfAccordion x:Name="OuterAccordion" ExpandMode="SingleOrNone" Margin="5" BindableLayout.ItemsSource="{Binding Info}"> <BindableLayout.ItemTemplate> <DataTemplate> <syncfusion:AccordionItem x:Name="AccordionItem"> <syncfusion:AccordionItem.Header> <Grid HeightRequest="50" RowSpacing="0"> <Label Text="{Binding Name}" VerticalOptions="Center" HorizontalOptions="StartAndExpand"/> </Grid> </syncfusion:AccordionItem.Header> <syncfusion:AccordionItem.Content> <sfListView:SfListView x:Name="listView" HeightRequest="{Binding Varieties, Converter={StaticResource HeightConverter}, ConverterParameter={x:Reference listView}}" ItemSize="50" ItemsSource="{Binding Varieties}" ItemSpacing="1"> <sfListView:SfListView.ItemTemplate> <DataTemplate> <Grid RowSpacing="0"> <Label Text="{Binding Name}" VerticalOptions="Center" HorizontalOptions="Start" Padding="2"/> <Label Grid.Column="1" Text="{Binding Price}" VerticalOptions="Center" HorizontalOptions="Start" Padding="2"/> </Grid> </DataTemplate> </sfListView:SfListView.ItemTemplate> </sfListView:SfListView> </syncfusion:AccordionItem.Content> </syncfusion:AccordionItem> </DataTemplate> </BindableLayout.ItemTemplate> </syncfusion:SfAccordion> </ContentPage.Content> </ContentPage>
C#
Converter class to return height based on items and SfListView.ItemsSize.
namespace AccordionXamarin.Converter { public class HeightConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var listView = parameter as SfListView; var items = value as ObservableCollection<Variety>; return items.Count * listView.ItemSize; } } }
Output
Conclusion
I hope you enjoyed learning about how to work with Accordion with SfListView in Xamarin.Forms (SfAccordion).
You can refer to our Xamarin.Forms Accordion feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
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!