■ TreeView 클래스를 사용해 디렉토리를 동적으로 탐색하는 방법을 보여준다.
▶ ResourceHelper.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 |
using System; using System.IO; using System.Windows; namespace TestProject { /// <summary> /// 리소스 헬퍼 /// </summary> public class ResourceHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 리소스 URI 구하기 - GetResourceUri(assemblyName, resourcePath) /// <summary> /// 리소스 URI 구하기 /// </summary> /// <param name="assemblyName">어셈블리명</param> /// <param name="resourcePath">리소스 경로</param> /// <returns>리소스 URI</returns> public static Uri GetResourceUri(string assemblyName, string resourcePath) { if(string.IsNullOrEmpty(assemblyName)) { return new Uri(string.Format("pack://application:,,,/{0}", resourcePath)); } else { return new Uri(string.Format("pack://application:,,,/{0};component/{1}", assemblyName, resourcePath)); } } #endregion #region 리소스 스트림 구하기 - GetResourceStream(resourceUri) /// <summary> /// 리소스 스트림 구하기 /// </summary> /// <param name="resourceUri">리소스 URI</param> /// <returns>리소스 스트림</returns> public static Stream GetResourceStream(Uri resourceUri) { return Application.GetResourceStream(resourceUri).Stream; } #endregion } } |
▶ ImagedTreeViewItem.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 |
using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace TestProject { /// <summary> /// 이미지 트리뷰 항목 /// </summary> public class ImageTreeViewItem : TreeViewItem { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 이미지 /// </summary> private Image image; /// <summary> /// 선택 이미지 소스 /// </summary> private ImageSource selectedImageSource; /// <summary> /// 미선택 이미지 소스 /// </summary> private ImageSource unselectedImageSource; /// <summary> /// 텍스트 블럭 /// </summary> private TextBlock textBlock; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 텍스트 - Text /// <summary> /// 텍스트 /// </summary> public string Text { set { this.textBlock.Text = value; } get { return this.textBlock.Text; } } #endregion #region 선택 이미지 소스 - SelectedImageSource /// <summary> /// 선택 이미지 소스 /// </summary> public ImageSource SelectedImageSource { set { this.selectedImageSource = value; if(IsSelected) { this.image.Source = this.selectedImageSource; } } get { return this.selectedImageSource; } } #endregion #region 미선택 이미지 소스 - UnselectedImageSource /// <summary> /// 미선택 이미지 소스 /// </summary> public ImageSource UnselectedImageSource { set { this.unselectedImageSource = value; if(!IsSelected) { this.image.Source = this.unselectedImageSource; } } get { return this.unselectedImageSource; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ImageTreeViewItem() /// <summary> /// 생성자 /// </summary> public ImageTreeViewItem() { StackPanel stackPanel = new StackPanel(); stackPanel.Orientation = Orientation.Horizontal; Header = stackPanel; this.image = new Image(); this.image.Margin = new Thickness(0, 0, 2, 0); this.image.VerticalAlignment = VerticalAlignment.Center; stackPanel.Children.Add(this.image); this.textBlock = new TextBlock(); this.textBlock.VerticalAlignment = VerticalAlignment.Center; stackPanel.Children.Add(this.textBlock); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 선택시 처리하기 - OnSelected(e) /// <summary> /// 선택시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnSelected(RoutedEventArgs e) { base.OnSelected(e); this.image.Source = this.selectedImageSource; } #endregion #region 미선택시 처리하기 - OnUnselected(e) /// <summary> /// 미선택시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnUnselected(RoutedEventArgs e) { base.OnUnselected(e); this.image.Source = this.unselectedImageSource; } #endregion } } |
▶ DirectoryTreeViewItem.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 |
using System.IO; using System.Windows; using System.Windows.Media.Imaging; namespace TestProject { /// <summary> /// 디렉토리 트리뷰 항목 /// </summary> public class DirectoryTreeViewItem : ImageTreeViewItem { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 디렉토리 정보 /// </summary> private DirectoryInfo directoryInfo; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 디렉토리 정보 - DriectoryInfo /// <summary> /// 디렉토리 정보 /// </summary> public DirectoryInfo DirectoryInfo { get { return this.directoryInfo; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - DirectoryTreeViewItem(directoryInfo) /// <summary> /// 생성자 /// </summary> /// <param name="directoryInfo">디렉토리 정보</param> public DirectoryTreeViewItem(DirectoryInfo directoryInfo) { this.directoryInfo = directoryInfo; Text = directoryInfo.Name; SelectedImageSource = new BitmapImage(ResourceHelper.GetResourceUri("TestProject", "Image/OpenFolder.bmp" )); UnselectedImageSource = new BitmapImage(ResourceHelper.GetResourceUri("TestProject", "Image/CloseFolder.bmp")); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 전개하다 - Populate() /// <summary> /// 전개하다 /// </summary> public void Populate() { DirectoryInfo[] directoryInfoArray; try { directoryInfoArray = this.directoryInfo.GetDirectories(); } catch { return; } foreach(DirectoryInfo pDirectoryInfo in directoryInfoArray) { Items.Add(new DirectoryTreeViewItem(pDirectoryInfo)); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 확장시 처리하기 - OnExpanded(e) /// <summary> /// 확장시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnExpanded(RoutedEventArgs e) { base.OnExpanded(e); foreach(object item in Items) { DirectoryTreeViewItem directoryTreeViewItem = item as DirectoryTreeViewItem; directoryTreeViewItem.Populate(); } } #endregion } } |
▶ DirectoryTreeView.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 |
using System; using System.IO; using System.Windows.Controls; using System.Windows.Media.Imaging; namespace TestProject { /// <summary> /// 디렉토리 트리뷰 /// </summary> public class DirectoryTreeView : TreeView { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - DirectoryTreeView() /// <summary> /// 생성자 /// </summary> public DirectoryTreeView() { Populate(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 전개하다 - Populate() /// <summary> /// 전개하다 /// </summary> public void Populate() { BeginInit(); Items.Clear(); DriveInfo[] driveInfoArray = DriveInfo.GetDrives(); foreach(DriveInfo driveInfo in driveInfoArray) { char driveCharacter = driveInfo.Name.ToUpper()[0]; DirectoryTreeViewItem directoryTreeViewItem = new DirectoryTreeViewItem(driveInfo.RootDirectory); if(driveCharacter != 'A' && driveCharacter != 'B' && driveInfo.IsReady && driveInfo.VolumeLabel.Length > 0) { directoryTreeViewItem.Text = String.Format("{0} ({1})", driveInfo.VolumeLabel, driveInfo.Name); } else { directoryTreeViewItem.Text = String.Format("{0} ({1})", driveInfo.DriveType, driveInfo.Name); } if(driveCharacter == 'A' || driveCharacter == 'B') { BitmapImage bitmapImage = new BitmapImage(ResourceHelper.GetResourceUri("TestProject", "Image/Floppy.bmp")); directoryTreeViewItem.SelectedImageSource = bitmapImage; directoryTreeViewItem.UnselectedImageSource = bitmapImage; } else if(driveInfo.DriveType == DriveType.CDRom) { BitmapImage bitmapImage = new BitmapImage(ResourceHelper.GetResourceUri("TestProject", "Image/CDDrive.bmp")); directoryTreeViewItem.SelectedImageSource = bitmapImage; directoryTreeViewItem.UnselectedImageSource = bitmapImage; } else { BitmapImage bitmapImage = new BitmapImage(ResourceHelper.GetResourceUri("TestProject", "Image/Drive.bmp")); directoryTreeViewItem.SelectedImageSource = bitmapImage; directoryTreeViewItem.UnselectedImageSource = bitmapImage; } Items.Add(directoryTreeViewItem); if(driveCharacter != 'A' && driveCharacter != 'B' && driveInfo.IsReady) { directoryTreeViewItem.Populate(); } } EndInit(); } #endregion } } |
▶ MainWindow.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 |
using System; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace TestProject { /// <summary> /// 테스트 윈도우 /// </summary> public class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 스택 패널 /// </summary> private StackPanel stackPanel; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { Width = 800d; Height = 600d; Title = "TreeView 클래스 : 디렉토리 동적 탐색하기"; FontFamily = new FontFamily("나눔고딕코딩"); FontSize = 16d; Grid grid = new Grid(); Content = grid; ColumnDefinition columnDefinition1 = new ColumnDefinition(); columnDefinition1.Width = new GridLength(50, GridUnitType.Star); grid.ColumnDefinitions.Add(columnDefinition1); ColumnDefinition columnDefinition2 = new ColumnDefinition(); columnDefinition2.Width = GridLength.Auto; grid.ColumnDefinitions.Add(columnDefinition2); ColumnDefinition columnDefinition3 = new ColumnDefinition(); columnDefinition3.Width = new GridLength(50, GridUnitType.Star); grid.ColumnDefinitions.Add(columnDefinition3); DirectoryTreeView directoryTreeView = new DirectoryTreeView(); directoryTreeView.SelectedItemChanged += directoryTreeView_SelectedItemChanged; grid.Children.Add(directoryTreeView); Grid.SetColumn(directoryTreeView, 0); GridSplitter gridSplitter = new GridSplitter(); gridSplitter.Width = 6; gridSplitter.ResizeBehavior = GridResizeBehavior.PreviousAndNext; grid.Children.Add(gridSplitter); Grid.SetColumn(gridSplitter, 1); ScrollViewer scrollViewer = new ScrollViewer(); grid.Children.Add(scrollViewer); Grid.SetColumn(scrollViewer, 2); this.stackPanel = new StackPanel(); scrollViewer.Content = this.stackPanel; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> [STAThread] public static void Main() { Application application = new Application(); application.Run(new MainWindow()); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region 디렉토리 트리뷰 선택 항목 변경시 처리하기 - directoryTreeView_SelectedItemChanged(sender, e) /// <summary> /// 디렉토리 트리뷰 선택 항목 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void directoryTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { DirectoryTreeViewItem directoryTreeViewItem = e.NewValue as DirectoryTreeViewItem; this.stackPanel.Children.Clear(); FileInfo[] fileInfoArray; try { fileInfoArray = directoryTreeViewItem.DirectoryInfo.GetFiles(); } catch { return; } foreach(FileInfo fileInfo in fileInfoArray) { TextBlock textBlock = new TextBlock(); textBlock.Text = fileInfo.Name; this.stackPanel.Children.Add(textBlock); } } #endregion } } |