■ TreeListView 클래스에서 동적으로 노드를 로드하는 방법을 보여준다.
▶ FileSystemHelper.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 |
using System.IO; namespace TestProject { /// <summary> /// 파일 시스템 도우미 /// </summary> public class FileSystemHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 논리 드라이브 배열 구하기 - GetLogicalDriveArray() /// <summary> /// 논리 드라이브 배열 구하기 /// </summary> /// <returns>논리 드라이브 배열</returns> public string[] GetLogicalDriveArray() { return Directory.GetLogicalDrives(); } #endregion #region 디렉토리 경로 배열 구하기 - GetDirectoryPathArray(parentDirectoryPath) /// <summary> /// 디렉토리 경로 배열 구하기 /// </summary> /// <param name="parentDirectoryPath">부모 디렉토리 경로</param> /// <returns>디렉토리 경로 배열</returns> public string[] GetDirectoryPathArray(string parentDirectoryPath) { return Directory.GetDirectories(parentDirectoryPath); } #endregion #region 파일 경로 배열 구하기 - GetFilePathArray(directoryPath) /// <summary> /// 파일 경로 배열 구하기 /// </summary> /// <param name="directoryPath">디렉토리 경로</param> /// <returns>파일 경로 배열</returns> public string[] GetFilePathArray(string directoryPath) { return Directory.GetFiles(directoryPath); } #endregion #region 디렉토리명 구하기 - GetDirectoryName(directoryPath) /// <summary> /// 디렉토리명 구하기 /// </summary> /// <param name="directoryPath">디렉토리 경로</param> /// <returns>디렉토리명</returns> public string GetDirectoryName(string directoryPath) { return new DirectoryInfo(directoryPath).Name; } #endregion #region 파일명 구하기 - GetFileName(filePath) /// <summary> /// 파일명 구하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <returns>파일명</returns> public string GetFileName(string filePath) { return new FileInfo(filePath).Name; } #endregion #region 파일 크기 구하기 - GetFileSize(filePath) /// <summary> /// 파일 크기 구하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <returns>파일 크기</returns> public long GetFileSize(string filePath) { return new FileInfo(filePath).Length; } #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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" Width="800" Height="600" Title="TreeListView 클래스 : 동적으로 노드 로드하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <dxg:GridControl Name="gridControl"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="Name" /> <dxg:GridColumn FieldName="ItemType" /> <dxg:GridColumn FieldName="Size" /> <dxg:GridColumn FieldName="FullName" /> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TreeListView Name="treeListView" NodeExpanding="treeListView_NodeExpanding" /> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window> |
▶ MainWindow.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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
using System.Windows; using DevExpress.Utils; using DevExpress.Xpf.Grid; using DevExpress.Xpf.Grid.TreeList; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 파일 시스템 도우미 /// </summary> private FileSystemHelper fileSystemHelper = new FileSystemHelper(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); InitializeDrive(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 트리 리스트 뷰 노드 확장시 처리하기 - treeListView_NodeExpanding(sender, e) /// <summary> /// 트리 리스트 뷰 노드 확장시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void treeListView_NodeExpanding(object sender, TreeListNodeAllowEventArgs e) { TreeListNode treeListNode = e.Node; if(treeListNode.Tag == null || (bool)treeListNode.Tag == false) { BuildChildNode(treeListNode); treeListNode.Tag = true; } } #endregion #region 드라이브 초기화 하기 - InitializeDrive() /// <summary> /// 드라이브 초기화 하기 /// </summary> public void InitializeDrive() { this.gridControl.BeginDataUpdate(); try { string[] driveArray = this.fileSystemHelper.GetLogicalDriveArray(); foreach(string drive in driveArray) { TreeListNode rootNode = new TreeListNode() { Content = new FileSystemItem(drive, "Drive", "<Drive>", drive) }; this.treeListView.Nodes.Add(rootNode); rootNode.IsExpandButtonVisible = DefaultBoolean.True; } } catch { } this.gridControl.EndDataUpdate(); } #endregion #region 자식 노드 구축하기 - BuildChildNode(parentNode) /// <summary> /// 자식 노드 구축하기 /// </summary> /// <param name="parentNode">부모 노드</param> private void BuildChildNode(TreeListNode parentNode) { this.gridControl.BeginDataUpdate(); BuildFolderChildNode(parentNode); BuildFileChildNode(parentNode); this.gridControl.EndDataUpdate(); } #endregion #region 파일 자식 노드 구축하기 - BuildFileChildNode(parentNode) /// <summary> /// 파일 자식 노드 구축하기 /// </summary> /// <param name="parentNode">부모 노드</param> private void BuildFileChildNode(TreeListNode parentNode) { FileSystemItem fileSystemItem = parentNode.Content as FileSystemItem; if(fileSystemItem == null) { return; } try { string[] filePathArray = this.fileSystemHelper.GetFilePathArray(fileSystemItem.FullName); foreach(string filePath in filePathArray) { TreeListNode childNode = new TreeListNode() { Content = new FileSystemItem ( this.fileSystemHelper.GetFileName(filePath), "File", this.fileSystemHelper.GetFileSize(filePath).ToString(), filePath ) }; childNode.IsExpandButtonVisible = DefaultBoolean.False; parentNode.Nodes.Add(childNode); } } catch { } } #endregion #region 폴더 자식 노드 구축하기 - BuildFolderChildNode(parentNode) /// <summary> /// 폴더 자식 노드 구축하기 /// </summary> /// <param name="parentNode">부모 노드</param> private void BuildFolderChildNode(TreeListNode parentNode) { FileSystemItem fileSystemItem = parentNode.Content as FileSystemItem; if(fileSystemItem == null) { return; } try { string[] directoryPathArray = this.fileSystemHelper.GetDirectoryPathArray(fileSystemItem.FullName); foreach(string directoryPath in directoryPathArray) { try { TreeListNode childNode = new TreeListNode() { Content = new FileSystemItem ( this.fileSystemHelper.GetDirectoryName(directoryPath), "Folder", "<Folder>", directoryPath ) }; parentNode.Nodes.Add(childNode); childNode.IsExpandButtonVisible = HasChildren(directoryPath) ? DefaultBoolean.True : DefaultBoolean.False; } catch { } } } catch { } } #endregion #region 자식 보유 여부 조사하기 - HasFiles(directoryPath) /// <summary> /// 자식 보유 여부 조사하기 /// </summary> /// <param name="directoryPath">디렉토리 경로</param> /// <returns>자식 보유 여부</returns> private bool HasChildren(string directoryPath) { string[] filePathArray = this.fileSystemHelper.GetFilePathArray(directoryPath); if(filePathArray.Length > 0) { return true; } string[] childDirectoryPathArray = this.fileSystemHelper.GetDirectoryPathArray(directoryPath); if(childDirectoryPathArray.Length > 0) { return true; } return false; } #endregion } } |