2

For a Silverlight 2 webapp. I added a combobox. I have an IEnumerable as Itemsource to populate the combobox. Works fine.

But I would like to add an extra item ("please select a....") to the combobox, anyone an idea how this can be done using the Silverlight 2 combobox.

Any more info about using a template for the ComboxboxItems is welcome as well.

4 Answers4

1

You can easily insert an item at a desired index location in the Items collection of the ComboBox using the following code.

          TextBlock t = new TextBlock();             t.Text = "Please select....";             combo.Items.Insert(0, t);

Setting the selected index will set the ComboBox to show your added item by default:

 combo.SelectedIndex = 0;
RobZ
  • 330
  • 3
  • 6
0

If you are using WCF, you can use the following code:

SilverlightApplication1.ServiceReference1.Region item = 
   new SilverlightApplication1.ServiceReference1.Region ();
item.RegionID = 0;
item.RegionDescription = "-Select Region-";
e.Result.Insert(0, item);

drControl.ItemsSource = e.Result; ////////.Result;  
drControl.SelectedIndex = 0; 
Donut
  • 110,061
  • 20
  • 134
  • 146
0

After returning a List<> from an asynchronous call to a WCF service I use the following syntax to add an item to the results before binding to the combo box. In my scenario if you try to add the item to the list after binding, a read-only error is thrown.

E.Result.Items.Insert(0, new object { param1 = "", Param2 = ""} );
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
-1

Just add that in Xaml page and make it selected to True....

Taran
  • 1