■ TreeView 엘리먼트의 ItemTemplateSelector 속성을 사용해 사진/음악 라이브러리 파일을 조회하는 방법을 보여준다.
※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다.
※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType 태그를 None으로 추가했다.
▶ ExplorerItemTemplateSelector.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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
using Windows.Storage; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace TestProject; /// <summary> /// 항목 탐색기 템플리트 선택자 /// </summary> public class ExplorerItemTemplateSelector : DataTemplateSelector { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 디폴트 템플리트 - DefaultTemplate /// <summary> /// 디폴트 템플리트 /// </summary> public DataTemplate DefaultTemplate { get; set; } #endregion #region 음악 항목 템플리트 - MusicItemTemplate /// <summary> /// 음악 항목 템플리트 /// </summary> public DataTemplate MusicItemTemplate { get; set; } #endregion #region 그림 항목 템플리트 - PictureItemTemplate /// <summary> /// 그림 항목 템플리트 /// </summary> public DataTemplate PictureItemTemplate { get; set; } #endregion #region 음악 폴더 템플리트 - MusicFolderTemplate /// <summary> /// 음악 폴더 템플리트 /// </summary> public DataTemplate MusicFolderTemplate { get; set; } #endregion #region 그림 폴더 템플리트 - PictureFolderTemplate /// <summary> /// 그림 폴더 템플리트 /// </summary> public DataTemplate PictureFolderTemplate { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 템플리트 선택하기 (코어) - SelectTemplateCore(item) /// <summary> /// 템플리트 선택하기 (코어) /// </summary> /// <param name="item">항목</param> /// <returns>데이터 템플리트</returns> protected override DataTemplate SelectTemplateCore(object item) { TreeViewNode treeViewNode = item as TreeViewNode; if(treeViewNode.Content is StorageFolder) { StorageFolder storageFolder = treeViewNode.Content as StorageFolder; if(storageFolder.DisplayName.StartsWith("Pictures")) { return PictureFolderTemplate; } if(storageFolder.DisplayName.StartsWith("Music")) { return MusicFolderTemplate; } } else if(treeViewNode.Content is StorageFile) { StorageFile storageFile = treeViewNode.Content as StorageFile; if(storageFile.ContentType.StartsWith("image")) { return PictureItemTemplate; } if(storageFile.ContentType.StartsWith("audio")) { return MusicItemTemplate; } } return DefaultTemplate; } #endregion } |
▶ MainPage.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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
<?xml version="1.0" encoding="utf-8"?> <Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ws="using:Windows.Storage" xmlns:local="using:TestProject" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <Page.Resources> <DataTemplate x:Key="DefaultTreeViewNodeDataTemplateKey" x:DataType="TreeViewNode"> <Grid Height="50"> <TextBlock Style="{ThemeResource BodyTextBlockStyle}" HorizontalAlignment="Left" VerticalAlignment="Center" Text="{x:Bind ((ws:IStorageItem)Content).Name}" /> </Grid> </DataTemplate> <DataTemplate x:Key="MusicItemTreeViewNodeDataTemplateKey" x:DataType="TreeViewNode"> <StackPanel Height="50" Orientation="Horizontal"> <SymbolIcon Margin="0 0 5 0" Symbol="Audio" /> <TextBlock Style="{ThemeResource BodyTextBlockStyle}" HorizontalAlignment="Left" VerticalAlignment="Center" Text="{x:Bind ((ws:StorageFile)Content).DisplayName}" /> </StackPanel> </DataTemplate> <DataTemplate x:Key="PictureItemTreeViewNodeDataTemplateKey" x:DataType="TreeViewNode"> <StackPanel Height="50" Orientation="Horizontal"> <FontIcon Margin="0 0 5 0" FontFamily="Segoe MDL2 Assets" Glyph="" /> <TextBlock Style="{ThemeResource BodyTextBlockStyle}" HorizontalAlignment="Left" VerticalAlignment="Center" Text="{x:Bind ((ws:StorageFile)Content).DisplayName}" /> </StackPanel> </DataTemplate> <DataTemplate x:Key="MusicFolderTreeViewNodeDataTemplateKey" x:DataType="TreeViewNode"> <StackPanel Height="50" Orientation="Horizontal"> <SymbolIcon Margin="0 0 5 0" Symbol="MusicInfo" /> <TextBlock Style="{ThemeResource BodyTextBlockStyle}" HorizontalAlignment="Left" VerticalAlignment="Center" Text="{x:Bind ((ws:StorageFolder)Content).DisplayName}" /> </StackPanel> </DataTemplate> <DataTemplate x:Key="PictureFolderTreeViewNodeDataTemplateKey" x:DataType="TreeViewNode"> <StackPanel Height="50" Orientation="Horizontal"> <SymbolIcon Margin="0 0 5 0" Symbol="Pictures" /> <TextBlock Style="{ThemeResource BodyTextBlockStyle}" HorizontalAlignment="Left" VerticalAlignment="Center" Text="{x:Bind ((ws:StorageFolder)Content).DisplayName}" /> </StackPanel> </DataTemplate> <local:ExplorerItemTemplateSelector x:Key="ExplorerItemTemplateSelectorKey" DefaultTemplate="{StaticResource DefaultTreeViewNodeDataTemplateKey}" MusicItemTemplate="{StaticResource MusicItemTreeViewNodeDataTemplateKey}" MusicFolderTemplate="{StaticResource MusicFolderTreeViewNodeDataTemplateKey}" PictureItemTemplate="{StaticResource PictureItemTreeViewNodeDataTemplateKey}" PictureFolderTemplate="{StaticResource PictureFolderTreeViewNodeDataTemplateKey}" /> </Page.Resources> <SplitView Margin="10" OpenPaneLength="300" DisplayMode="Inline" IsPaneOpen="True"> <SplitView.Pane> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Button Name="refreshButton" Grid.Row="0" Padding="10" Content="Refresh tree" /> <TreeView Name="treeView" Grid.Row="1" Margin="0 10 0 0" ItemTemplateSelector="{StaticResource ExplorerItemTemplateSelectorKey}" SelectionMode="Single" /> </Grid> </SplitView.Pane> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Center" Padding="10" Spacing="10"> <TextBlock Style="{StaticResource CaptionTextBlockStyle}" Text="File name : " /> <TextBlock Name="fileNameTextBlock" Margin="0 0 0 10" /> <TextBlock Style="{StaticResource CaptionTextBlockStyle}" Text="File path : " /> <TextBlock Name="filePathTextBlock" Margin="0 0 0 10" /> <TextBlock Style="{StaticResource CaptionTextBlockStyle}" Text="Tree depth : " /> <TextBlock Name="treeDepthTextBlock" Margin="0 0 0 10" /> </StackPanel> </SplitView> </Page> |
▶ MainPage.xaml.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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
using System; using System.Collections.Generic; using Windows.Storage; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.refreshButton.Click += refreshButton_Click; this.treeView.Expanding += treeView_Expanding; this.treeView.Collapsed += treeView_Collapsed; this.treeView.ItemInvoked += treeView_ItemInvoked; InitializeTreeView(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region Refresh 버튼 클릭시 처리하기 - refreshButton_Click(sender, e) /// <summary> /// Refresh 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void refreshButton_Click(object sender, RoutedEventArgs e) { this.treeView.RootNodes.Clear(); InitializeTreeView(); } #endregion #region 트리뷰 확장시 처리하기 - treeView_Expanding(sender, e) /// <summary> /// 트리뷰 확장시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void treeView_Expanding(TreeView sender, TreeViewExpandingEventArgs e) { if(e.Node.HasUnrealizedChildren) { AddChildTreeViewNode(e.Node); } } #endregion #region 트리뷰 축소시 처리하기 - treeView_Collapsed(sender, e) /// <summary> /// 트리뷰 축소시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void treeView_Collapsed(TreeView sender, TreeViewCollapsedEventArgs e) { e.Node.Children.Clear(); e.Node.HasUnrealizedChildren = true; } #endregion #region 트리뷰 항목 호출시 처리하기 - treeView_ItemInvoked(sender, e) /// <summary> /// 트리뷰 항목 호출시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void treeView_ItemInvoked(TreeView sender, TreeViewItemInvokedEventArgs e) { TreeViewNode treeViewNode = e.InvokedItem as TreeViewNode; if(treeViewNode.Content is IStorageItem storageItem) { this.fileNameTextBlock.Text = storageItem.Name; this.filePathTextBlock.Text = storageItem.Path; this.treeDepthTextBlock.Text = treeViewNode.Depth.ToString(); if(treeViewNode.Content is StorageFolder) { treeViewNode.IsExpanded = !treeViewNode.IsExpanded; } } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 트리뷰 초기화하기 - InitializeTreeView() /// <summary> /// 트리뷰 초기화하기 /// </summary> private void InitializeTreeView() { StorageFolder pictureStorageFolder = KnownFolders.PicturesLibrary; TreeViewNode pictureTreeViewNode = new TreeViewNode(); pictureTreeViewNode.Content = pictureStorageFolder; pictureTreeViewNode.IsExpanded = true; pictureTreeViewNode.HasUnrealizedChildren = true; this.treeView.RootNodes.Add(pictureTreeViewNode); AddChildTreeViewNode(pictureTreeViewNode); StorageFolder musicStorageFolder = KnownFolders.MusicLibrary; TreeViewNode musicTreeViewNode = new TreeViewNode(); musicTreeViewNode.Content = musicStorageFolder; musicTreeViewNode.IsExpanded = true; musicTreeViewNode.HasUnrealizedChildren = true; this.treeView.RootNodes.Add(musicTreeViewNode); AddChildTreeViewNode(musicTreeViewNode); } #endregion #region 자식 트리뷰 노드 추가하기 - AddChildTreeViewNode(parentTreeViewNode) /// <summary> /// 자식 트리뷰 노드 추가하기 /// </summary> /// <param name="parentTreeViewNode">부모 트리뷰 노드</param> private async void AddChildTreeViewNode(TreeViewNode parentTreeViewNode) { StorageFolder storageFolder = null; if(parentTreeViewNode.Content is StorageFolder && parentTreeViewNode.HasUnrealizedChildren == true) { storageFolder = parentTreeViewNode.Content as StorageFolder; } else { return; } IReadOnlyList<IStorageItem> storageItemList = await storageFolder.GetItemsAsync(); if(storageItemList.Count == 0) { return; } foreach(IStorageItem storageItem in storageItemList) { TreeViewNode childTreeViewNode = new TreeViewNode(); childTreeViewNode.Content = storageItem; if(storageItem is StorageFolder) { childTreeViewNode.HasUnrealizedChildren = true; } parentTreeViewNode.Children.Add(childTreeViewNode); } parentTreeViewNode.HasUnrealizedChildren = false; } #endregion } |