【问题标题】:Adding default Item in ComboBox在 ComboBox 中添加默认项
【发布时间】:2016-01-26 21:14:55
【问题描述】:

绑定后,我想在0th IndexCombobox 中添加一个默认项。我试着这样做:

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

但出现错误:

只读集合不支持操作

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

【问题讨论】:

    标签: c# silverlight combobox


    【解决方案1】:

    但是

    我假设您正在使用 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;
    

    类似的东西。

    【讨论】:

    • Result.Add 不接受两个参数
    • 好的,e.Result的类型是什么?
    • ObservableCollection&lt;ProductType&gt;ProductType是我的班级
    • Combobox 不包含Text 的定义。不,我正在使用Silverlight
    • 在我假设您使用的是 WinForms 之前,抱歉。我更新我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2013-05-23
    • 1970-01-01
    相关资源
    最近更新 更多