Articles in this section

How to group items in suggestion box in Xamarin.Forms Autocomplete control

Grouping in DropDown is possible with the help of SfListView, which is added in the DropDownHeaderView or DropDownFooterView of SfAutoComplete.

 

In SfAutoComplete, you can view the grouped data with the help of SfListView ItemsSource being bound to the FilteredCollection of SfAutoComplete in the DropDownHeaderView or DropDownFooterView.

In the following example, the grouping is achieved in SfListView and we have added the SfListView in the DropDownHeaderView of SfAutoComplete.

 

Code snippets [Xaml]

 

<ContentPage.Content>
        <StackLayout Padding="20">
            <autoComplete:SfAutoComplete x:Name="autoComplete" Grid.Row="0" DataSource="{Binding contactsinfo}" DisplayMemberPath="ContactName" DropDownHeaderViewHeight="100" DropDownItemHeight="0" FilteredItems="{Binding FilteredCollection, Mode=TwoWay}" HeightRequest="40" NoResultsFoundTextColor="Gray" SelectedItem="{Binding SelectionObject}" SuggestionBoxPlacement="Bottom">
                <autoComplete:SfAutoComplete.DropDownHeaderView>
                    <syncfusion:SfListView x:Name="listView" BackgroundColor="AliceBlue" HeightRequest="800" ItemSpacing="1" ItemTapped="listView_ItemTapped" ItemsSource="{Binding FilteredCollection}" SelectedItem="{Binding SelectionObject}" SelectionMode="Single">
                        <syncfusion:SfListView.ItemTemplate>
                            <DataTemplate>
                                <Grid x:Name="grid" RowSpacing="0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="1" />
                                    </Grid.RowDefinitions>
                                    <Grid RowSpacing="0">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="70" />
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
 
                                        <Image HeightRequest="50" HorizontalOptions="Center" Source="{Binding ContactImage}" VerticalOptions="Center" WidthRequest="50" />
 
                                        <Grid Grid.Column="1" Padding="10,0,0,0" RowSpacing="1" VerticalOptions="Center">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="*" />
                                                <RowDefinition Height="*" />
                                            </Grid.RowDefinitions>
 
                                            <Label LineBreakMode="NoWrap" Text="{Binding ContactName}" TextColor="#474747" />
                                            <Label Grid.Row="1" Grid.Column="0" LineBreakMode="NoWrap" Text="{Binding ContactNumber}" TextColor="#474747" />
                                        </Grid>
                                        <Grid Grid.Row="0" Grid.Column="2" HorizontalOptions="End" RowSpacing="0" VerticalOptions="Start">
 
                                            <Label LineBreakMode="NoWrap" Text="{Binding ContactType}" TextColor="#474747" />
                                        </Grid>
                                    </Grid>
                                </Grid>
                            </DataTemplate>
                        </syncfusion:SfListView.ItemTemplate>
                    </syncfusion:SfListView>
                </autoComplete:SfAutoComplete.DropDownHeaderView>
            </autoComplete:SfAutoComplete>
        </StackLayout>
    </ContentPage.Content>

 

 

Code snippets [C#]

 

public MainPage()
        {
            InitializeComponent();
            contactsViewModel = new ContactsViewModel();
            listView.DataSource.GroupDescriptors.Add(new GroupDescriptor()
            {
                PropertyName = "ContactName",
                KeySelector = (object obj1) =>
                {
                    var item = (obj1 as Contacts);
                    return item.ContactName[0].ToString();
                }
            });
 
            listView.SelectionChanged += ListView_SelectionChanged;
            autoComplete.SelectionChanged += AutoComplete_SelectionChanged;
            this.BindingContext = contactsViewModel;
        }

 

 

 

Output


A screenshot of a computer screen

Description automatically generated

 

Please refer the sample link for reference.

 

 

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