使用 ItemsSource 将预定义项目添加到 ComboBox

Adi*_*dir 4 c# data-binding wpf combobox

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

(Select item)
Item 1
Item 2
Item 3
Run Code Online (Sandbox Code Playgroud)

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

Rik*_*kki 5

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

<ComboBox>
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <ListBoxItem>Please Select</ListBoxItem>
            <CollectionContainer Collection="{Binding Source={StaticResource YOURDATASOURCE}}" />
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

以下是一些向您展示 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?