KrystianB
KrystianB

Reputation: 502

WPF Combobox isEditable bind entry to list

I have a combobox, I want the user to be able to enter their selection into the combobox. I have it set to editable which works but the user can enter any entry they want. Though say the list contains Dog, Cat, Bird. The user can enter Snaked instead. How can I stop this?

        <ComboBox IsEditable="True" Grid.Column="2" Grid.Row="1" Margin="5,3,0,0"
                  Text="{Binding Model.Number}" ItemsSource="{Binding DList}"
                  SelectedItem="{Binding Model.Number}"
                  IsEnabled="{Binding EnableComboBox}" 
                  VerticalAlignment="Top">

        </ComboBox>

Upvotes: 2

Views: 486

Answers (2)

mm8
mm8

Reputation: 169220

How can I stop this?

Set the IsEditable property to false and the IsTextSearchEnabled property to true:

<ComboBox IsEditable="False" ItemsSource="{Binding DList}" IsTextSearchEnabled="True" />

It doesn't make much sense to have an editable ComboBox with a TextBox if you only want the user to be able to select something that is actually present in the Items collection.

If the ComboBox is bound to an IEnumerable<T> you could set the TextSearch.TextPath property to the name of a property of the type T:

<ComboBox IsEditable="False" ItemsSource="{Binding Animals}" IsTextSearchEnabled="True" TextSearch.TextPath="AnimalName" />

Upvotes: 0

Mokey
Mokey

Reputation: 215

Make it Read only, this will stop the typing feature. If in VS you can do this in the properties section of the combo box.

Upvotes: 1

Related Questions