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. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get SelectedText from AutoComplete

Platform: Xamarin.Forms |
Control: SfAutoComplete |
Published Date: September 19, 2018 |
Last Revised Date: June 21, 2019

Step 1: Create a custom autocomplete control.

Step 2: Use a separate property in the view model file to bind the selected item and use another property to get the selected text.

Step 3: Inherit the INotifyPropertyChanged class in view model class.

Step 4: Implement the PropertyChangedEventHandler interface. The property changed event will be raised whenever any property is changed.

Step 5: Create the RaisePropertyChanged method to raise the property changed event.

Step 6: Call the RaisePropertyChanged method inside the Selected item property, which will call the selected text property whenever the selected item property is updated.

Step 7: In the selected text property, get the current selected item text from the autocomplete selected item property.

 

The following code demonstrates the how to get selected text from auto complete control.

XAML

<?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:SfAutocompleteSample"
             xmlns:input="clr-namespace:Syncfusion.SfAutoComplete.XForms;assembly=Syncfusion.SfAutoComplete.XForms"
             xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
             x:Class="SfAutocompleteSample.MainPage">
    <ContentPage.BindingContext>
        <local:ViewModel/>
  </ContentPage.BindingContext>
    
  <ContentPage.Content>
 
        <StackLayout>
            <input:SfAutoComplete x:Name="AutoComplete2" 
                                  BackgroundColor="Bisque"
                                  HeightRequest="100"
                                  SelectedItem="{Binding SelectionObject}"
                                  DataSource="{Binding Colors}"
                                  MultiSelectMode="Token"
                                  BorderColor="Black"                                 
                                  IsSelectedItemsVisibleInDropDown="False"
                                  AutoCompleteMode="SuggestAppend"
                                  WatermarkColor="Blue"
                                  ShowClearButton="True"
                                  SuggestionBoxPlacement="Top"
                                  NoResultsFoundText="No Services Found"
                                  SuggestionMode="Contains"
                                  TextHighlightMode="MultipleOccurrence"
                                  HighlightedTextColor="Red"
                                  HighlightedTextFontAttributes="Bold"
                                  DisplayMemberPath="Name"                                 
                                  TextColor="Black">
            </input:SfAutoComplete>
            <Label Text="Selected Values are" 
                   Margin="0,10"/>
            <ListView x:Name="list2"  ItemsSource="{Binding SelectedValue}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="{Binding}" />
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
 
</ContentPage>

 

C#

namespace SfAutocompleteSample
{
    #region Mainpage Class
 
    public partial class MainPage : ContentPage
    {
        #region Constructor
 
        public MainPage()
        {
            InitializeComponent();         
        }
 
        #endregion
 
    }
 
    #endregion
 
    public class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
            Colors = new ObservableCollection<Model>();
            Colors.Add(new Model() { ID = "1", Name = "Red" });
            Colors.Add(new Model() { ID = "2", Name = "Pink" });
            Colors.Add(new Model() { ID = "3", Name = "Orange" });
            Colors.Add(new Model() { ID = "4", Name = "Lavendar" });
            Colors.Add(new Model() { ID = "5", Name = "Violet" });
            Colors.Add(new Model() { ID = "6", Name = "Indigo" });
            Colors.Add(new Model() { ID = "7", Name = "White" });
            Colors.Add(new Model() { ID = "8", Name = "Green" });
            Colors.Add(new Model() { ID = "9", Name = "Blue" });
            Colors.Add(new Model() { ID = "10", Name = "Yellow" });
            SelectionObject.Add(Colors[1]);
     
        }
 
        private ObservableCollection<object> selectionobject = new ObservableCollection<object>();
        public ObservableCollection<object> SelectionObject
        {
            get { return selectionobject; }
            set
            {
                selectionobject = value;
                RaisePropertyChanged("SelectionObject");
                RaisePropertyChanged("SelectedValue");
            }
        }
 
        private ObservableCollection<string> selectedvalue = new ObservableCollection<string>();
 
        public ObservableCollection<string> SelectedValue
        {
            get
            {
                selectedvalue.Clear();
 
                foreach (var item in SelectionObject)
                {
                    selectedvalue.Add((item as Model).Name.ToString());
                }
 
                return selectedvalue;
            }
            set
            {
                selectedvalue = value;
 
            }
        }
 
 
        private ObservableCollection<Model> _colors;
        public ObservableCollection<Model> Colors
        {
            get { return _colors; }
            set
            {
                _colors = value;
                RaisePropertyChanged("Colors");
            }
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        public void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
 
    public class Model 
    {
        private string id;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
 
    }
}

 

The following screenshot illustrates customized return keyboard.

 

SelectedText.png

 

Please find the sample from the below link:

 

http://www.syncfusion.com/downloads/support/forum/139491/ze/SfAutocompleteSample_GetText1775718278

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
pau
Jun 03, 2019

Hi, i want a clear item in the listview. That delete the selection value from the selescted items.

Reply

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