在 ComboBox 中添加默认项

Adding default Item in ComboBox

我想在绑定后在Combobox at 0th Index 添加一个默认项目。我试着这样做:

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

但出现错误:

Operation Not Supported on Read-only collection

Silverlight ComboBox中添加默认项的方法是什么?

但是

我假设您正在使用 WPF 并且 XAML 试试这个:

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>

但是

如果您像您所说的那样使用 Silverlight:

使用:NotSelectedText="--Select Products--"

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

源代码:local:ExtendedComboBox 开启:

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

Silverlight: Default value in Combobox

另类

是用文本 "--Select Products--" 创建默认值 ProductType 然后添加到集合 oj 位置 0;

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

cboProductType.ItemsSource = e.Result;

类似的东西。