Asix Jin
Asix Jin

Reputation: 119

How to get a ComboBox to only autocomplete to what is in drop down

Basically what I am trying to do is have it where you can type something into a ComboBox and it autocompletes to something from it's drop down, but the user shouldn't be able to enter their own entry.

I know that by default if you have "isEditable" equal to true then it autocompletes on it's own. However you can still enter in whatever you want. I want to prevent this.

This is how I am pulling my names in

void populateNames()
    {
        nameBox = this.nameTextBox;
        APICaller Caller = new APICaller();
        try
        {
            List<string> listOfNames = Caller.APIGetNames();
            foreach (string a in listOfNames)
            {
                Console.WriteLine(a);
                nameBox.Items.Add(a);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Something went wrong: " + e);
        }
        nameBox.SelectedIndex = 0;
    }

and the ComboBox in the XAML

<ComboBox 
        Name="nameTextBox" Height="23" Width="Auto" Margin="10,0,10,97" VerticalAlignment="Bottom" IsEditable="True"
        PreviewTextInput="tbxPreviewTextInput" DataObject.Pasting="tbxPasting" LostFocus="nameTbxLostFocus" 
        GotFocus="nameTbxGotFocus" PreviewKeyDown="classTextBox_PreviewKeyDown"  HorizontalAlignment="Stretch"/>

Upvotes: 0

Views: 78

Answers (1)

cyi
cyi

Reputation: 96

Under your properties tab, set your AutoComplete source to the list of items that populates the combo box. Then, set your AutoCompleteMode to Suggest or SuggestAppend.

Upvotes: 1

Related Questions