How to group items in suggestion box in .NET MAUI Autocomplete Control?
In .NET MAUI Autocomplete, the control provides a simple and effective way to display a list of suggestions based on user input. This article will guide you through the process of grouping items within the suggestion box of the .NET MAUI Autocomplete control.
Step 1: Define ViewModel
In your ViewModel (EmployeeViewModel), organize your employee data into groups. This may involve creating a property for each group and categorizing employees accordingly.
C#
public class EmployeeViewModel
{
public ObservableCollection<Employee> Employees { get; set; }
public EmployeeViewModel()
{
this.Employees = new ObservableCollection<Employee>();
Employees.Add(new Employee {
Name = "Anne Dodsworth",
ProfilePicture = "people_circle1.png",
Designation = "Developer",
ID = "E001",
});
Employees.Add(new Employee {
Name = "Andrew Fuller",
ProfilePicture = "people_circle2.png",
Designation = "Team Lead",
ID = "E002",
});
Employees.Add(new Employee {
Name = "Emilio Alvaro",
ProfilePicture = "people_circle4.png",
Designation = "Product Manager",
ID = "E003",
});
Employees.Add(new Employee {
Name = "Janet Lavering",
ProfilePicture = "people_circle5.png",
Designation = "HR",
ID = "E004",
});
}
}
Step 2: Update XAML to Bind to Employees
Define the Autocomplete in your XAML file. Modify the Autocomplete XAML to bind to the Employees property in the ViewModel, and use the TextSearchMode property for searching items within the groups.
XAML
<editors:SfAutocomplete Placeholder="Enter an employee"
TextSearchMode="StartsWith"
DisplayMemberPath="Name"
ItemsSource="{Binding Employees}"
WidthRequest="280"
HeightRequest="34"
x:Name="autoComplete">
<editors:SfAutocomplete.BindingContext>
<local:EmployeeViewModel/>
</editors:SfAutocomplete.BindingContext>
<editors:SfAutocomplete.ItemTemplate>
<DataTemplate >
<ViewCell>
<Grid Margin="0,5"
VerticalOptions="Center"
HorizontalOptions="Center"
ColumnDefinitions="48,220"
RowDefinitions="50">
<Image Grid.Column="0"
HorizontalOptions="Center"
VerticalOptions="Center"
Source="{Binding ProfilePicture}"
Aspect="AspectFit"/>
<StackLayout HorizontalOptions="Start"
VerticalOptions="Center"
Grid.Column="1"
Margin="15,0,0,0">
<Label HorizontalTextAlignment="Start"
VerticalTextAlignment="Center"
Opacity=".87"
FontSize="14"
Text="{Binding Name}"/>
<Label HorizontalOptions="Start"
VerticalTextAlignment="Center"
Opacity=".54"
FontSize="12"
Text="{Binding Designation}"/>
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</editors:SfAutocomplete.ItemTemplate>
</editors:SfAutocomplete>
Output
Conclusion
Hope you enjoyed learning about how to group items in the suggestion box in .NET MAUI Autocomplete control.
You can refer to our .NET MAUI AutoComplete’s feature tour page to learn about its other groundbreaking feature representations. You can explore our .NET MAUI AutoComplete documentation to understand how to present and manipulate data.
For current customers, you can check out our .NET MAUI 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 .NET MAUI AutoComplete (SfAutoComplete) and other .NET MAUI components.
Please let us know in the comments section below if you have any queries or require clarifications. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!