Yannic
Yannic

Reputation: 229

WPF : Nullable ComboBox

I want to have an empty item in the comboBox to allow the user to "Unselect" and keep the comboBox empty (Null value).

How can I do that?

Upvotes: 6

Views: 2974

Answers (2)

Kent Boogaart
Kent Boogaart

Reputation: 178640

Make your life easier by using a sentinel value. That is, an instance of your view model class that represents nothing.

Upvotes: 3

Pete OHanlon
Pete OHanlon

Reputation: 9146

If you take a look at my blog entry here, you can see a binding solution that doesn't require you to "modify" your VM or to add dummy items into a collection that doesn't really fit with your data.

Basically, you use a CompositeCollection in your XAML, which gives you the ability (for instance) to have numeric values in your combo-box, and the text "Please select..." to designate the place holder, which you can't do if you are binding entirely to numeric fields in your model and relying on that to add this magic value.

Upvotes: 0

Related Questions