How to populate the items dynamically in suggestion list in AutoComplete?
Xamarin.Forms AutoComplete provides support for populating the items dynamically in suggestion list.
To populate the items dynamically in suggestion list:
Step 1: Add the required assemblies to get the view of AutoComplete.
Step 2: Provide a method in which you load the items to add in suggestion list, dynamically.
Step 3: Call that method inside the value changed event so that value in the suggestion list be updated whenever the change occurs in the entered value.
The following code illustrates how to achieve this method.
XAML code:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:AutoComplete" xmlns:auto="clr-namespace:Syncfusion.SfAutoComplete.XForms;assembly=Syncfusion.SfAutoComplete.XForms" x:Class="AutoComplete.MainPage"> <ContentPage.Content> <StackLayout> <autocomplete:SfAutoComplete x:Name="AutoComplete" DataSource="{Binding AutoCompleteItems,Mode=TwoWay}" SelectionChanged="AutoComplete_OnSelectionChanged" DisplayMemberPath="Label" SelectedValuePath="Id" ValueChanged="AutoComplete_OnValueChanged" Watermark="test"/> </StackLayout> </ContentPage.Content> </ContentPage>
Code behind code:
namespace AutoComplete { public class AutoCompleteItem { public int Id { get; set; } public string Label { get; set; } } public partial class MainPage : ContentPage { private SfAutoCompletePageViewModel context; public MainPage() { InitializeComponent(); context = new SfAutoCompletePageViewModel(); BindingContext = context; } private void AutoComplete_OnSelectionChanged(object sender, Syncfusion.SfAutoComplete.XForms.SelectionChangedEventArgs e) { var value = (e?.Value as AutoCompleteItem)?.Label; if (string.IsNullOrEmpty(value)) return; } private void AutoComplete_OnValueChanged(object sender, Syncfusion.SfAutoComplete.XForms.ValueChangedEventArgs e) { var text = e?.Value; if (string.IsNullOrEmpty(text)) return; context?.RefreshAutoCompleteDataSourceItems(text); } } }
Code behind ViewModel code:
namespace AutoComplete { public class SfAutoCompletePageViewModel : INotifyPropertyChanged { #region INotifyPropertyChanged Implementation public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged([CallerMemberName] string propertyName = "") { if (PropertyChanged == null) return; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion private ObservableCollection<AutoCompleteItem> autoCompleteItems; public ObservableCollection<AutoCompleteItem> AutoCompleteItems { get { return autoCompleteItems; } set { autoCompleteItems = value; OnPropertyChanged(); } } public SfAutoCompletePageViewModel() { AutoCompleteItems = new ObservableCollection<AutoCompleteItem>() { new AutoCompleteItem() { Id = 1, Label = "salut" } }; } public async Task RefreshAutoCompleteDataSourceItems(string text) { var items = new ObservableCollection<AutoCompleteItem>(); for (int i = 0; i < 10; i++) { items.Add(new AutoCompleteItem() { Id = i, Label = text + i }); } AutoCompleteItems = items; } } }
Image after the Value gets updated dynamically:
|
Conclusion
I hope you enjoyed learning about how to populate the items dynamically in suggestion list in AutoComplete.
You can refer to our Xamarin.Forms Autocomplete feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Forms Autocomplete documentation to understand how to create and manipulate data.
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!