This is a migrated thread and some comments may be shown as answers.

How to set SelectedItem

5 Answers 256 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sapan
Top achievements
Rank 1
Sapan asked on 09 Dec 2018, 06:55 PM

RadAutoCompleteBox has a very useful property which is "SelectedItem". We can get the value of the chosen item from it. The problem is you can only get it. But what if we want to set the value of the selected item programmatically? Is there anything like "SelectedIndex", "SelectedValue", "Selecteditem"?

There is RadAutoCompleteBox.Text, but this only display the text and doesn't set the selected item.

5 Answers, 1 is accepted

Sort by
0
Lance | Senior Manager Technical Support
Telerik team
answered on 10 Dec 2018, 05:56 PM
Hi Sapan,

The SelectedItem can only be set by the user, when picking something from the FilteredItems fly-out. This is by design, as the Text property is the primary 'value' property (this is what can be set programmatically or two-way bound to).

Programmatic Selection

If you want to programmatically make a selection, just set the Text property using one of the items from the data source. For example, let's say the AutoComplete is bound to a list of Person object and you wanted "Alfredo" to be pre-selected.

var peopleDataSource = new ObservableCollection<Person>(new SampleDataService().GeneratePeopleData(true));
 
radAutoCompleteBox.ItemsSource = peopleDataSource;
 
// Selecting an item programmatically -  I want Alfredo to be the selected item
var result = peopleDataSource.FirstOrDefault(p => p.Name.Contains("Alfredo"));
 
if(result != null)
{
    radAutoCompleteBox.Text = result.Name;
}

I've attached a demo that uses the above code. When you run the demo, you'll see that Alfredo is the preselected item when the page loads.


Side Note:

If you ever needed to get the object that matches the test, just do the reverse of what we did to set the text:

var person = peopleDataSource.FirstOrDefault(p => p.Name.Contains(radAutoCompleteBox.Text));


Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Sapan
Top achievements
Rank 1
answered on 13 Dec 2018, 01:52 PM
This is not the best way I guess. For example, what if we have two or more persons with the exact name of "Alfredo"?
0
Lance | Senior Manager Technical Support
Telerik team
answered on 13 Dec 2018, 03:11 PM
Hi Sapan,

My example is just to demonstrate that you can set the Text property, not the SelectedItem property. Such code examples not intended for use in production applications or copy/pasted directly.How you go about setting the Text property is up to your application's business logic needs.

To address your question directly, what do you do if there are two matches for a query? You could concatenate the string values of you wanted a multiple selection-like behavior. Just keep in mind that the AutoCompleteBox does not select an instance of the entity object, it uses a string for its value.

If you need an input control that selects instances of entities, then I recommend switching to a ComboBox or ListView type of control.

Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Sapan
Top achievements
Rank 1
answered on 17 Dec 2018, 10:54 AM

Thanks for your reply. Yes, we know how to handle this scenario.

But if you add an option for your AutoCompleteBox to set SelectedItem programmatically, it would be very useful. Because UWP Combobox doesn't have an option for filtering until the recent update (1809) which is not complete and backward incompatible.

0
Lance | Senior Manager Technical Support
Telerik team
answered on 17 Dec 2018, 05:30 PM
Hi Sapan,

I have created an official feature request: AutoCompleteBox - Set SelectedItem. Please take a moment to up-vote, follow and comment on the item so that the development team fully understands your use case and desire for the feature over just setting the Text.

Thank you for your feedback, I hope my approach is a suitable workaround in the meantime.

Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
AutoCompleteBox
Asked by
Sapan
Top achievements
Rank 1
Answers by
Lance | Senior Manager Technical Support
Telerik team
Sapan
Top achievements
Rank 1
Share this question
or