■ CompositeCollection 엘리먼트를 사용해 다수 컬렉션과 항목을 하나의 목록으로 만드는 방법을 보여준다.
▶ GreekGod.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
namespace TestProject { /// <summary> /// 그리스 신 /// </summary> public class GreekGod { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이름 - Name /// <summary> /// 이름 /// </summary> public string Name { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - GreekGod(name) /// <summary> /// 생성자 /// </summary> /// <param name="name">이름</param> public GreekGod(string name) { Name = name; } #endregion } } |
▶ GreekGodCollection.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System.Collections.ObjectModel; namespace TestProject { /// <summary> /// 그리스 신 컬렉션 /// </summary> public class GreekGodCollection : ObservableCollection<GreekGod> { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - GreekGodCollection() /// <summary> /// 생성자 /// </summary> public GreekGodCollection() { Add(new GreekGod("Aphrodite" )); Add(new GreekGod("Apollo" )); Add(new GreekGod("Ares" )); Add(new GreekGod("Artemis" )); Add(new GreekGod("Athena" )); Add(new GreekGod("Demeter" )); Add(new GreekGod("Dionysus" )); Add(new GreekGod("Hephaestus")); Add(new GreekGod("Hera" )); Add(new GreekGod("Hermes" )); Add(new GreekGod("Poseidon" )); Add(new GreekGod("Zeus" )); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:GreekGodCollection x:Key="GreekGodCollectionKey" /> <XmlDataProvider x:Key="XmlDataProviderKey" XPath="GreekHeroes/Hero"> <x:XData> <GreekHeroes xmlns=""> <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 local:GreekGod}"> <TextBlock Foreground="Goldenrod" FontWeight="Bold" Text="{Binding Path=Name}" /> </DataTemplate> <DataTemplate DataType="Hero"> <TextBlock Foreground="RoyalBlue" FontWeight="Bold" Text="{Binding XPath=@Name}" /> </DataTemplate> </Window.Resources> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock HorizontalAlignment="Center" Margin="10" FontSize="20" FontWeight="Bold"> Composite Collections Sample </TextBlock> <ListBox Width="200" Height="300" Background="White"> <ListBox.ItemsSource> <CompositeCollection> <CollectionContainer Collection="{Binding Source={StaticResource GreekGodCollectionKey}}" /> <CollectionContainer Collection="{Binding Source={StaticResource XmlDataProviderKey}}" /> <ListBoxItem Foreground="Red" FontWeight="Bold"> Other Listbox Item 1 </ListBoxItem> <ListBoxItem Foreground="Red" FontWeight="Bold"> Other Listbox Item 2 </ListBoxItem> </CompositeCollection> </ListBox.ItemsSource> </ListBox> </StackPanel> </Window> |