-1

I want to add a default Item in Combobox at 0th Index after I bind it. I tried to do it like this:

cboProductType.ItemsSource = e.Result;
cboProductType.Items.Insert(0, "--Select Products--");  //error on this line

but got error:

Operation Not Supported on Read-only collection

What's the way of adding default items in Silverlight ComboBox?

Huma Ali
  • 1,759
  • 7
  • 40
  • 66

1 Answers1

1

BUT

I assume You are using WPF and XAML try this:

WPF Combobox DefaultValue (Please Select)

<ComboBox SelectedIndex="0">
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <ListBoxItem>Please Select</ListBoxItem>
            <CollectionContainer Collection="{Binding Source={StaticResource YOURDATASOURCE}}" />
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>

BUT

If You are using Silverlight like You said:

Use: NotSelectedText="--Select Products--"

<local:ExtendedComboBox ItemsSource="{Binding ...Whatever...}" NotSelectedText="--Select Products--" />

Source code of: local:ExtendedComboBox is on:

https://vortexwolf.wordpress.com/2011/04/05/silverlight-combobox-prompt-text-if-an-item-isnt-selected/

Silverlight: Default value in Combobox

Alterntive

Is create the default ProductType with text "--Select Products--" and then add to colection oj position 0;

e.Result.Add(new ProductType { Text =" --Select Products-- " });

cboProductType.ItemsSource = e.Result;

Something like that.

Community
  • 1
  • 1
blogprogramisty.net
  • 1,714
  • 1
  • 18
  • 22