2X faster development
The ultimate Xamarin UI toolkit to boost your development speed.
Auto complete 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:
|
2X faster development
The ultimate Xamarin UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.