Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have editable combobox where not always the preferred item is in the drop-down list.

I would like to have the possibility of manually entering text in the textbox which is propagated to the string bound to SelectedValue.

Right now the string bound to SelectedValue is only updated if the entered value is on of the ones in the ComboBox items.

How do I allow custom values not available in the ComboBox list to be manually entered and properly propagated to bound value?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
418 views
Welcome To Ask or Share your Answers For Others

1 Answer

I was just doing this yesterday and today and it looks like the following:

  1. set the combobox IsEditable="true"

  2. instead of binding to SelectedItem, bind to the Text property of the combobox

  3. if you're binding to a custom object instead of just strings, you need to also set TextSearch.TextPath="NameOfField". This lets the text search behavior work, and also shows this property in the textbox as well.

All in all, I ended up with something like:

<ComboBox x:Name="c" 
          IsEditable="True" 
          IsTextSearchEnabled="True" 
          IsTextSearchCaseSensitive="False" 
          StaysOpenOnEdit="True"
          Text="{Binding NameOnViewModel}"
          TextSearch.TextPath="NameOnChildItems"  
          ItemsSource="{Binding Items}" 
          ItemTemplate="{StaticResource DataTemplate}" />

<TextBlock Text="{Binding ElementName=c,Path=Text}" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...