방법: CompositeCollection 구현
업데이트: 2007년 11월
예제
다음 예제에서는 CompositeCollection 클래스를 사용하여 여러 컬렉션 및 항목을 하나의 목록으로 표시하는 방법을 보여 줍니다. 이 예제에서 GreekGods는 GreekGod 사용자 지정 개체의 ObservableCollection<T>입니다. GreekGod 개체와 GreekHero 개체가 황금색 및 녹청 전경색으로 각각 나타나도록 데이터 템플릿이 정의됩니다.
<Window Background="Cornsilk"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:SDKSample"
x:Class="SDKSample.Window1"
Title="CompositeCollections"
SizeToContent="WidthAndHeight"
>
<Window.Resources>
<c:GreekGods x:Key="GreekGodsData"/>
<XmlDataProvider x:Key="GreekHeroesData" XPath="GreekHeroes/Hero">
<x:XData>
<GreekHeroes >
<Hero Name="Jason" />
<Hero Name="Hercules" />
<Hero Name="Bellerophon" />
<Hero Name="Theseus" />
<Hero Name="Odysseus" />
<Hero Name="Perseus" />
</GreekHeroes>
</x:XData>
</XmlDataProvider>
<DataTemplate DataType="{x:Type c:GreekGod}">
<TextBlock Text="{Binding Path=Name}" Foreground="Gold"/>
</DataTemplate>
<DataTemplate DataType="Hero">
<TextBlock Text="{Binding XPath=@Name}" Foreground="Cyan"/>
</DataTemplate>
</Window.Resources>
<StackPanel>
<TextBlock FontSize="18" FontWeight="Bold" Margin="10"
HorizontalAlignment="Center">Composite Collections Sample</TextBlock>
<ListBox Name="myListBox" Height="300" Width="200" Background="White">
<ListBox.ItemsSource>
<CompositeCollection>
<CollectionContainer
Collection="{Binding Source={StaticResource GreekGodsData}}" />
<CollectionContainer
Collection="{Binding Source={StaticResource GreekHeroesData}}" />
<ListBoxItem Foreground="Red">Other Listbox Item 1</ListBoxItem>
<ListBoxItem Foreground="Red">Other Listbox Item 2</ListBoxItem>
</CompositeCollection>
</ListBox.ItemsSource>
</ListBox>
</StackPanel>
</Window>