You could add two DataTemplate resources, one for each type A
and B
, without x:Key
, just DataType
so they're used implicitly as default templates. The dual collection thing can be handled by a CompositeCollection
.
This is how it'd work schematically:
<ListBox>
<ListBox.Resources>
<CollectionViewSource x:Key="ListOfA" Source="{Binding ListOfA}"/>
<CollectionViewSource x:Key="ListOfB" Source="{Binding ListOfB}"/>
<DataTemplate DataType="{x:Type local:A}">
<TextBlock Text="{Binding Path=Name }"/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:B}">
<TextBlock Text="{Binding Path=Value}"/>
</DataTemplate>
</ListBox.Resources>
<ListBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource ListOfA}}" />
<CollectionContainer Collection="{Binding Source={StaticResource ListOfB}}" />
</CompositeCollection>
</ListBox.ItemsSource>
</ListBox>
ViewModel (just for the Demo) => DataContext of the ListBox
public class MainViewModel
{
public MainViewModel (){
ListOfA = new List<A> { new A { Name = "Susi" }, new A { Name = "Bertie" }, new A { Name = "Anna" } };
ListOfB = new List<B> { new B { Value = 600 }, new B { Value = 700 }, new B { Value = 1000 } };
}
public List<A> ListOfA { get; }
public List<B> ListOfB { get; }
}
public class A
{
public string Name { get; set;}
}
public class B
{
public int Value { get; set; }
}