How to search both Accordion and ListView in Xamarin.Forms?
You can search for both SfAccordion and SfListView loaded inside AccordionItem.Content in Xamarin.Forms. To load the SfListView inside Accordion, refer to the article from here.
XAML
SearchBar is loaded at the top of the page to search for SfAccordion. The SfListView is loaded inside the BindableLayout.ItemTemplate.
<ContentPage xmlns="http://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" xmlns:helper="clr-namespace:AccordionXamarin.Helper" x:Class="AccordionXamarin.MainPage" Padding="{OnPlatform iOS='0,40,0,0'}"> ... <ContentPage.Content> <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> <SearchBar x:Name="searchBar" HeightRequest="50"/> <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" SelectedItem="{Binding SelectedItem}" HeightRequest="{Binding Varieties, Converter={StaticResource HeightConverter}, ConverterParameter={x:Reference listView}}" ItemSize="50" ItemsSource="{Binding Varieties}" ItemSpacing="1"> <sfListView:SfListView.ItemTemplate> <DataTemplate> ... </DataTemplate> </sfListView:SfListView.ItemTemplate> </sfListView:SfListView> </syncfusion:AccordionItem.Content> </syncfusion:AccordionItem> </DataTemplate> </BindableLayout.ItemTemplate> </syncfusion:SfAccordion> </StackLayout> </ContentPage.Content> </ContentPage>
C#
In the SearchBar.TextChanged event, filter items based on the NewTextValue property of TextChangedEventArgs. You can filter items based on the collection bound to both the Accordion and the ListView. Filtered items set to Accordion using the BindableLayout.SetItemsSource method. You can expand the AccordionItem to show the searched ListViewItem by setting the IsExpanded property to true.
namespace AccordionXamarin.Helper { public class Behavior : Behavior<ContentPage> { SfAccordion Accordion; SearchBar SearchBar; protected override void OnAttachedTo(ContentPage bindable) { Accordion = bindable.FindByName<SfAccordion>("OuterAccordion"); SearchBar = bindable.FindByName<SearchBar>("searchBar"); SearchBar.TextChanged += SearchBar_TextChanged; base.OnAttachedTo(bindable); } private void SearchBar_TextChanged(object sender, TextChangedEventArgs e) { var viewModel = new ItemInfoRepository(); var accordionItems = viewModel.Info; if (string.IsNullOrEmpty(e.NewTextValue)) { BindableLayout.SetItemsSource(Accordion, accordionItems); return; } var filteredSource = accordionItems.Where(x => (x.Name.ToLower()).StartsWith(e.NewTextValue.ToLower())).ToList<ItemInfo>(); if(filteredSource.Count == 0) { List<Variety> listfilteredSource = null; for (int i = 0; i < accordionItems.Count; i++) { var item = accordionItems[i]; listfilteredSource = item.Varieties.Where(x => (x.Name.ToLower()).StartsWith(e.NewTextValue.ToLower())).ToList<Variety>(); if (listfilteredSource.Count > 0) { var list = accordionItems.Where(x => x.Name.Contains(item.Name.ToString())); BindableLayout.SetItemsSource(Accordion, list); if (!Accordion.Items[0].IsExpanded) Accordion.Items[0].IsExpanded = true; return; } } } BindableLayout.SetItemsSource(Accordion, filteredSource); } } }
Output
Conclusion
I hope you enjoyed learning about how to search for both Accordion and ListView in Xamarin.Forms
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!