可编辑的组合框绑定到值不在列表中 [英] Editable ComboBox with binding to value not in list

查看:163
本文介绍了可编辑的组合框绑定到值不在列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有可编辑的组合框,其中并不总是首选项在下拉列表中。



我想有可能在文本框中手动输入文本,并将其传播到绑定到SelectedValue的字符串。



现在,绑定到SelectedValue的字符串只有在输入的值是ComboBox项中的值时才会更新。



如何允许自定义值

解决方案

我只是在昨天和今天做这个它如下所示:


  1. 设置组合框 IsEditable =true SelectedItem ,而不是绑定到 Text



  2. 如果绑定到自定义对象而不是字符串,您还需要设置 TextSearch.TextPath =NameOfField


总而言之,我最终得到了像:

 < 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}/>


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?

解决方案

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}" />

这篇关于可编辑的组合框绑定到值不在列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆