Editable ComboBox with binding to value not in list

后端 未结 2 2067
小蘑菇
小蘑菇 2020-11-30 23:06

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

相关标签:
2条回答
  • 2020-11-30 23:14

    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}" />
    
    0 讨论(0)
  • 2020-11-30 23:33

    Setting the binding to Text property of Combo will suffice as well.

    <ComboBox  IsTextSearchEnabled="True"    IsEditable="True" 
    ItemsSource="{Binding Items}" Text="{Binding SelectedItemText, Mode=TwoWay}" />
    
    0 讨论(0)
提交回复
热议问题