除了其他子集合之外,有没有办法将单个项添加到CompositeCollection(用作ComboBox的源)?
这就是我所拥有的:
<ComboBox x:Name="combo">
<ComboBox.Resources>
<CollectionViewSource x:Key="CollectionAsAProperty" Source="{Binding CollectionA, Mode=TwoWay}" SelectedItem="{Binding CurrentItem}" />
</ComboBox.Resources>
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={x:Static local:MyViewModel.StaticCollection}}" />
<CollectionContainer Collection="{Binding Source={StaticResource CollectionAsAProperty}}"/>
===> what should I add here to add another item of DataContext.AnotherItem
</CompositeCollection>
</ComboBox.ItemsSource>
<ComboBox.ItemTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
编辑:与
有点合作<ObjectDataProvider ObjectInstance="{x:Static local:MyViewModel.AnotherItem}"/>
但是,(1)它需要一个静态属性,并且附加命令中的(2)(为简洁起见未在此处显示),所选项目的类型返回的是ObjectDataProvider
,而不是MyViewModel.AnotherItem
的类型,这会导致ViewModel中的一些痛苦
答案 0 :(得分:0)
将复合集合移动到viewModel,并使用后面代码中的Add method添加所有不同的集合。
var myAnimals = new List<Animal>() { new Animal() { Name = "Zebra" },
new Animal() { Name = "Cobra" }} ;
var myPeople = new List<Person>() { new Person() { Name = "Snake Pliskin" },
new Person() { Name = "OmegaMan" }};
var FavoriteThings = new CompositeCollection();
FavoriteThings.Add(myAnimals);
FavoriteThings.Add(myPeople);
public class Animal
{
public string Name { get; set; }
}
public class Person
{
public string Name { get; set; }
}