使用 ItemsSource 将预定义的项目添加到 ComboBox [英] Adding predefined item to a ComboBox with ItemsSource

查看:21
本文介绍了使用 ItemsSource 将预定义的项目添加到 ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将预定义的 ComboBoxItem 添加到我的 ComboBox 中,该 ComboBox 已经设置了 ItemsSource 属性.例子:

I'm trying to add a predefined ComboBoxItem into my ComboBox which already has a ItemsSource property set. example:

(Select item)
Item 1
Item 2
Item 3

是否可以在不修改原始项目集合的情况下执行此操作?

It's possible to do this without modifying the original items collection?

推荐答案

下面是一些来自 MSDN 的示例代码,展示了 CompositeCollection 的用法:

Here is some sample code from MSDN that shows the usage of CompositeCollection:

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

这里有一些参考资料向您展示了 CompositeCollection 的用法:

Here are some references to show you the usage of CompositeCollection:

1- http://msdn.microsoft.com/en-us/library/system.windows.data.compositecollection.aspx

2- http://robertbouillon.com/2010/04/17/adding-items-to-a-data-bound-wpf-combobox/

3- 我如何转换ComboBox 使用绑定的 CompositeCollection 吗?

这篇关于使用 ItemsSource 将预定义的项目添加到 ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆