)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to populate the items dynamically in suggestionlist in SfAutoComplete

Platform: Xamarin.Forms |
Control: SfAutoComplete |
Published Date: February 28, 2018 |
Last Revised Date: August 5, 2019

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:

Populating the items dynamically in SfAutoComplete

 

 

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon