English 中文(简体)
Wpf Combobox Limit to List
原标题:

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first item to match the letters typed as the selected item

The challenge is handling the scenario when the user enters characters that are NOT in the list. This should indicate that the user has either made a mistake OR wants to enter a new item.
The Combobox is bound to custom types and they have validation rules associated with the property being populated with the selection. We cannot figure out how to identify when the user has typed in an entry NOT IN THE LIST and set the bound property to an invalid value that will trigger the validation.

When a user enters a string of characters that do not represent an item in the list, the selecteditem becomes null. Since the selecteditem is null, the property path bound to the forms datacontext object cannot be identified by WPF and so the binding fails. The business object then retains whatever previous value was set and does not get a new value.

问题回答

Now that I spent a good half hour investigating a similar problem, the answer is quite easy: evaluate both the SelectedItem and the Text property of the ComboBox. The selected item will be null if the user did not select a value from the list; in this case text will contain the user s input. Depending on your needs you can either use binding to one or both properties or evaluate them in the code behind.





相关问题
WPF Datagrid, Setting the background of combox popup

I would like to change the color of the popup background when using a DatagridComboboxColumn in the WPF Toolkit datagrid. I ve edited the Template for a normal Combobox and it works great for selected ...

How to insert ComboBox item into ListBox? [winforms]

The question is very simple, How to insert ComboBox selected item into ListBox using c#? I have tried with this: listbox.Items.Add(combobox.SelectedItem); and some other permutations but it always ...

How do I bind a ComboBox to a one column list

I ve seen how to bind a ComboBox to a list that has columns like this: ItemsSource="{Binding Path=Entries}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path=Entry}" But ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

热门标签