views:

273

answers:

1

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.

+1  A: 

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.

David Schmitt