views:

789

answers:

3

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?

+1  A: 

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

HTH, Kent

Kent Boogaart
It's not exactly what i'm looking for. I want the selected item to be 'Null', not an instance like any other item of the combobox.
Yannic
A: 

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.

Pete OHanlon
I think I have to disagree Pete, and not just because you called me a “professional developer” ;) This is just way more work than using simple sentinels, and a lot of it pollutes the view. Don't get me wrong, I think it's certainly a valid option. But I guess I prefer the simplicity of sentinels. Also, since a VM is a model of the view, if the view needs something to represent nothing I do not think it's out of place being manifested in the VM. My 2p.
Kent Boogaart
Suppose you have a list of Countries, your 'sentinel' must be a country with no description ? So you are polluting the database. Maybe I don't understand something.
Yannic
If you are using the VM to bind from, then you don't actually need to put the data into the database - you just need to add it in before you set the DataContext.While I understand Kent's POV here, I have to disagree with him because this limits the sentinel to being compatible with the datatype of the data you're binding to. Suppose (as I stated above) that you want to display a list of integers which the user can select from, but you want to display "Please select..." to notify the user that a selection is required, you have to change the ints to strings.
Pete OHanlon
Understand your POV too, Pete. Thanks for the follow up. Regarding your example, I would actually ask what those ints represent. Ages? AgeViewModel. Waist size? WaistSizeViewModel. I always bind to values indirectly through a VM, even if that VM starts out very simple. I find this increases the readbility and long term flexibility of the code at very little cost. So I might have an AgeViewModel.Unspecified static field which is rendered in the view as "Please specify..."
Kent Boogaart
As always Kent, with MVVM, we go with what works for us.;-> I will say that my method works outside MVVM as well (gosh - did I just advocate a none-MVVM solution?)
Pete OHanlon
A: 

Read my blog for a fully working example of a clean nullable combobox, see http://www.blueedge.nl/weblog/post/Nullable-ComboBox-in-Silverlight.aspx

The solutions supports 2-way databinding with nullable items.

Jeroen