Articles in this section
Category / Section

How to get SelectedItem from .NET MAUI Autocomplete (SfAutocomplete)?

3 mins read

This article guides you through the process of getting the SelectedItem from the .NET MAUI Autocomplete (SfAutocomplete) control. You can acquire the selected item using the SelectionChanged event.

Steps

Step 1: Enable the SelectionChanged event in SfAutocomplete

In the XAML page, add the SelectionChanged event to the Autocomplete. Here, we will set the selected item’s value to the text property of a label.

<VerticalStackLayout Margin="10,0" Spacing="5">
    <Label Text="Choose country name" FontAttributes="Bold" FontSize="16"/>
    <editors:SfAutocomplete x:Name="autocomplete"
                            SelectionChanged="OnSelectionChanged">
        <editors:SfAutocomplete.ItemsSource>
            <x:Array Type="{x:Type x:String}">
                <x:String>United States</x:String>
                <x:String>India</x:String>
                <x:String>France</x:String>
                <x:String>Italy</x:String>
                <x:String>Germany</x:String>
                <x:String>China</x:String>
            </x:Array>
        </editors:SfAutocomplete.ItemsSource>
    </editors:SfAutocomplete>
    <HorizontalStackLayout>
        <Label Text="Country: " FontSize="16" FontAttributes="Bold"/>
        <Label x:Name="selectedText" FontSize="16"/>
    </HorizontalStackLayout>
</VerticalStackLayout>

Step 2: Get the text in the SelectionChanged event

The SelectionChanged event contains the newly selected item in the CurrentSelection property.

private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.CurrentSelection.Count > 0 && e.CurrentSelection[0] != null)
    {
        selectedText.Text = e.CurrentSelection[0].ToString();
    }
    else
    {
        selectedText.Text = string.Empty;
    }
}

Output

AutocompleteClearNew.gif

Conclusion
I hope you enjoyed learning about how to get the SelectedItem from the .NET MAUI AutoComplete (SfAutocomplete).
You can refer to our .NET MAUI Autocomplete’s feature tour page to learn about its other groundbreaking features. You can explore our .NET MAUI Autocomplete documentation to understand how to present and manipulate data.

For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore 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 clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied