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

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

问题描述

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

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

我希望能够在文本框中手动输入文本,该文本会传播到绑定到 SelectedValue 的字符串.

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

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

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

如何允许手动输入 ComboBox 列表中不可用的自定义值并正确传播到绑定值?

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. 设置组合框IsEditable="true"

绑定到组合框的Text属性,而不是绑定到SelectedItem

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

如果您绑定到自定义对象而不仅仅是字符串,您还需要设置 TextSearch.TextPath="NameOfField".这让文本搜索行为起作用,并且还在文本框中显示此属性.

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天全站免登陆