■ TreeList 클래스의 BeforeExpand 이벤트를 사용해 언바운드 모드의 데이터를 동적으로 로딩하는 방법을 보여준다.
▶ MainForm.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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
using System.IO; using System.Windows.Forms; using DevExpress.Utils; using DevExpress.XtraEditors; using DevExpress.XtraTreeList; using DevExpress.XtraTreeList.Columns; using DevExpress.XtraTreeList.Nodes; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); TreeListColumn fullNameColumn = new TreeListColumn(); fullNameColumn.FieldName = "FullName"; fullNameColumn.Caption = "FullName"; TreeListColumn nameColumn = new TreeListColumn(); nameColumn.FieldName = "Name"; nameColumn.Caption = "Name"; nameColumn.MinWidth = 30; nameColumn.Width = 250; nameColumn.VisibleIndex = 0; TreeListColumn typeColumn = new TreeListColumn(); typeColumn.FieldName = "Type"; typeColumn.Caption = "Type"; typeColumn.Width = 100; typeColumn.VisibleIndex = 1; TreeListColumn sizeColumn = new TreeListColumn(); sizeColumn.FieldName = "Size"; sizeColumn.Caption = "Size(Bytes)"; sizeColumn.AppearanceCell.Options.UseTextOptions = true; sizeColumn.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far; sizeColumn.Format.FormatType = FormatType.Numeric; sizeColumn.Format.FormatString = "n0"; sizeColumn.Width = 100; sizeColumn.VisibleIndex = 2; this.treeList.Columns.AddRange ( new TreeListColumn[] { fullNameColumn, nameColumn, typeColumn, sizeColumn } ); this.treeList.OptionsView.AutoWidth = false; this.treeList.OptionsBehavior.AutoNodeHeight = false; this.treeList.OptionsBehavior.Editable = false; this.treeList.StateImageList = this.imageList; this.treeList.BeforeExpand += treeList_BeforeExpand; this.treeList.AfterExpand += treeList_AfterExpand; this.treeList.AfterCollapse += treeList_AfterCollapse; AddFolderNode("C:\\", null); this.treeList.BestFitColumns(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 트리 리스트 확장 전 처리하기 - treeList_BeforeExpand(sender, e) /// <summary> /// 트리 리스트 확장 전 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void treeList_BeforeExpand(object sender, BeforeExpandEventArgs e) { if(e.Node.Tag != null) { Cursor cursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; AddFolderNode(e.Node.GetDisplayText("FullName"), e.Node); e.Node.Tag = null; Cursor.Current = cursor; } } #endregion #region 트리 리스트 확장 후 처리하기 - treeList_AfterExpand(sender, e) /// <summary> /// 트리 리스트 확장 후 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void treeList_AfterExpand(object sender, NodeEventArgs e) { if(e.Node.StateImageIndex != 1) { e.Node.StateImageIndex = 2; } } #endregion #region 트리 리스트 축소 후 처리하기 - treeList_AfterCollapse(sender, e) /// <summary> /// 트리 리스트 축소 후 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void treeList_AfterCollapse(object sender, NodeEventArgs e) { if(e.Node.StateImageIndex != 1) { e.Node.StateImageIndex = 0; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 파일 소유 여부 구하기 - HasFile(parentPath) /// <summary> /// 파일 소유 여부 구하기 /// </summary> /// <param name="parentPath">부모 경로</param> /// <returns>파일 소유 여부</returns> private bool HasFile(string parentPath) { string[] pathArray = Directory.GetFiles(parentPath); if(pathArray.Length > 0) { return true; } pathArray = Directory.GetDirectories(parentPath); if(pathArray.Length > 0) { return true; } return false; } #endregion #region 파일 노드 추가하기 - AddFileNode(parentPath, parentNode) /// <summary> /// 파일 노드 추가하기 /// </summary> /// <param name="parentPath">부모 경로</param> /// <param name="parentNode">부모 노드</param> private void AddFileNode(string parentPath, TreeListNode parentNode) { TreeListNode node; FileInfo fileInfo; try { string[] pathArray = Directory.GetFiles(parentPath); foreach(string path in pathArray) { fileInfo = new FileInfo(path); node = this.treeList.AppendNode ( new object[] { path, fileInfo.Name, "File", fileInfo.Length }, parentNode ); node.StateImageIndex = 1; node.HasChildren = false; } } catch { } } #endregion #region 폴더 노드 추가하기 - AddFolderNode(parentPath, parentNode) /// <summary> /// 폴더 노드 추가하기 /// </summary> /// <param name="parentPath">부모 경로</param> /// <param name="parentNode">부모 노드</param> private void AddFolderNode(string parentPath, TreeListNode parentNode) { this.treeList.BeginUnboundLoad(); TreeListNode node; DirectoryInfo directoryInfo; try { string[] pathArray = Directory.GetDirectories(parentPath); foreach(string path in pathArray) { try { directoryInfo = new DirectoryInfo(path); node = treeList.AppendNode ( new object[] { path, directoryInfo.Name, "Folder", null }, parentNode ); node.StateImageIndex = 0; node.HasChildren = HasFile(path); if(node.HasChildren) { node.Tag = true; } } catch { } } } catch { } AddFileNode(parentPath, parentNode); this.treeList.EndUnboundLoad(); } #endregion } } |