C# 使用ItemsSource将预定义项添加到组合框
我正在尝试将预定义的ComboBoxItem添加到已设置ItemsSource属性的ComboBox中。例如:C# 使用ItemsSource将预定义项添加到组合框,c#,wpf,data-binding,combobox,C#,Wpf,Data Binding,Combobox,我正在尝试将预定义的ComboBoxItem添加到已设置ItemsSource属性的ComboBox中。例如: (Select item) Item 1 Item 2 Item 3 可以在不修改原始items集合的情况下执行此操作?以下是MSDN中的一些示例代码,显示了CompositeCollection的用法: <ComboBox> <ComboBox.ItemsSource> <CompositeCollection>
(Select item)
Item 1
Item 2
Item 3
可以在不修改原始items集合的情况下执行此操作?以下是MSDN中的一些示例代码,显示了CompositeCollection的用法:
<ComboBox>
<ComboBox.ItemsSource>
<CompositeCollection>
<ListBoxItem>Please Select</ListBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource YOURDATASOURCE}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
请选择
以下是一些说明CompositeCollection用法的参考资料:
<ComboBox>
<ComboBox.ItemsSource>
<CompositeCollection>
<ListBoxItem>Please Select</ListBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource YOURDATASOURCE}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
1-
2-
3-如果要动态更改源项的内容,请改用ObservableCollection,这样您就可以访问Add()方法
private observeCollection myStrings;
公共MyClass()
{
myStrings=新的ObservableCollection();
myControl.ItemsSource=myString;
}
私有void AddNewItem(字符串项)
{
添加(项目);
}