简体   繁体   中英

How to emulate “AppendDataBoundItems” in Silverlight's ComboBox?

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.

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" eg '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};
  }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM