views:

410

answers:

2

In Asp.Net Combobox, there is a useful property, AppendDataBoundItems, that causes whatever items are bound to the control to be actually appended to whatever "statically" added ad design time. This is useful for "default" values that the user can specify when no item in the available ones meet the criteria, or to specify a special "null value" item.

Unfortunately there is no such property in Silverlight ComboBox and there is no way either to be notified when the control has been databound.

Any idea?

A: 

You may be able to do something by overriding the ComboBox template. However, the Silverlight combobox has many issues as it is an immature component. You may be better off implementing your own combobox (or using one of the implementations found on the Internet) and writing this behaviour yourself.

Jeff Yates
Thanks Jeff! interestingly enough I'm having the same problem these days with another application. I'll think of something on the weekend! thanks again
elsharpo
@elsharpo: Note that this answer was given before SL3 and 4. In SL4, the combo seems a lot more polished.
Jeff Yates
Thanks Jeff. I'll check it out when I get some breathing space from my other tasks!
elsharpo
A: 

I came accross the same problem just in the last few days for Comboboxes that were not mandatory. The way I dealt with it was to add a null value to the collection of say "Salutations" e.g 'Mr','Miss' and so forth.

Ok, my solution is ugly but it works.. Ideally I would like to have a bindableobject of type T that wraps the ObservableCollection. But as always, we have massive time pressures here and this will do for the time being.

Added a SalutationDTO to the ObservableCollection as follows.

 public static void EnableNullableSalutationChoice(this ObservableCollection<SalutationDTO> salutations)
{
  salutations.Insert(0, NullSalutationChoice);
}

    public static SalutationDTO NullSalutationChoice
{
  get
  {
    return new SalutationDTO {Salutation = " ", SalutationID = null};
  }
}
elsharpo
This is more or less the same solution I have been using when I have needed selectable null values in comboboxes and such. It seems to be the only half-decent way at the moment unless you want to build your own combobox from scratch. Keep in mind that this solution gets annoying when you need to pass a collection back to the service layer. Then you need to manually remove the "null" item before submitting it. Ugly, but it works.
Henrik Söderlund