How to get selecteditem from .NET MAUI AutoComplete (SfAutoComplete)?
In this article, we will guide you through the process of getting the SelectedItem from the .NET MAUI AutoComplete [SfAutoComplete] control. You can get the selected item using the SelectionChanged Event.
Steps
Step 1: Enable the SelectionChanged event in SfAutoComplete
In the Xaml page, add the SelectionChanged
event in AutoComplete.
Here, I am going to 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[0] != null)
selectedText.Text = e.CurrentSelection[0].ToString();
else
selectedText.Text = string.Empty;
}
Output
Conclusion
Hope you enjoyed learning about how to get 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 feature representations. You can explore our .NET MAUI AutoComplete documentation to understand how to present and manipulate data.
For current customers, you can check out our .NET MAUI 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 .NET MAUI AutoComplete(SfAutoComplete) and other .NET MAUI components.
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!