■ HierarchicalDataTemplate 엘리먼트의 DataType 속성을 사용해 트리를 만드는 방법을 보여준다.
▶ Sample.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 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 |
using System.Collections.ObjectModel; namespace TestProject { /// <summary> /// 샘플 /// </summary> public class Sample { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 자식 컬렉션 /// </summary> private ObservableCollection<string> childCollection = new ObservableCollection<string>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public string Name { get; set; } #endregion #region 자식 컬렉션 - ChildCollection /// <summary> /// 자식 컬렉션 /// </summary> public ObservableCollection<string> ChildCollection { get { return this.childCollection; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Sample() /// <summary> /// 생성자 /// </summary> public Sample() { for(int i = 0; i < 10; ++i) { ChildCollection.Add($"하위 샘플 {i}"); } } #endregion } } |
▶ SampleCollection.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 |
using System.Collections.ObjectModel; namespace TestProject { /// <summary> /// 샘플 컬렉션 /// </summary> public class SampleCollection : ObservableCollection<Sample> { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SampleCollection() /// <summary> /// 생성자 /// </summary> public SampleCollection() { for(int i = 0; i < 100; ++i) { Sample sample = new Sample(); sample.Name = "샘플 " + i.ToString(); Add(sample); } } #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 |
<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:SampleCollection x:Key="SampleCollectionKey" /> <HierarchicalDataTemplate DataType="{x:Type local:Sample}" ItemsSource="{Binding Path=ChildCollection}"> <TextBlock Text="{Binding Path=Name}" /> <HierarchicalDataTemplate.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}" /> </DataTemplate> </HierarchicalDataTemplate.ItemTemplate> <HierarchicalDataTemplate.ItemContainerStyle> <Style TargetType="TreeViewItem"> <Setter Property="Foreground" Value="Navy" /> </Style> </HierarchicalDataTemplate.ItemContainerStyle> </HierarchicalDataTemplate> </Window.Resources> <TreeView Margin="10" ItemsSource="{Binding Source={StaticResource SampleCollectionKey}}"> <TreeView.ItemContainerStyle> <Style TargetType="TreeViewItem"> <Setter Property="Foreground" Value="Green" /> <Setter Property="IsExpanded" Value="True" /> </Style> </TreeView.ItemContainerStyle> </TreeView> </Window> |
※ TreeView 엘리먼트에 아래 첨부 속성을 설정하면 테스트시 오류가 발생해서 프로그램이 비정상 종료된다.
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"