Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editable ComboBox with binding to value not in list

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?

like image 1000
ronag Avatar asked Sep 18 '10 19:09

ronag


People also ask

How do I make a combobox editable?

In this article we will see how we can make a combo box such that user and change the value of it by typing. By default when we create a combo box we can only choose from the option in the drop down menu although in editable combo box we can set the text by our self.

How to make combobox Editable in WPF?

Implementation. 1. Create an XAML with two columns, name and departments, where the 'departments' column has a datatemplate of an editable combobox. The user can edit and add new items to it, as mentioned in XAML.

What is combobox in WPF?

Advertisements. A combobox is a selection control that combines a non-editable textbox and a drop-down listbox that allows users to select an item from a list. It either displays the current selection or is empty if there is no selected item.


1 Answers

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}" /> 
like image 188
John Gardner Avatar answered Sep 22 '22 17:09

John Gardner