Tree 가 펼쳐질 때 하위 데이터를 불러오는 기법이 LazyLoading 임.
이하는 디스크 파일 탐색기를 트리로 구현한걸 보고 저장해놓음.
1 | <TreeView x:Name="trvDrive" Grid.Column="2" TreeViewItem.Expanded="trvDrive_Expanded"/> | 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 | void LoadData() { DriveInfo[] drives = DriveInfo.GetDrives(); foreach(var item in drives) trvDrive.Items.Add(CreateTreeViewItem(item)); } private TreeViewItem CreateTreeViewItem(object o) { TreeViewItem item = new TreeViewItem(); item.Header = o.ToString(); item.Tag = o; item.Items.Add("Loading..."); return item; } private void trvDrive_Expanded(object sender, RoutedEventArgs e) { Debug.WriteLine(sender.GetType()); TreeViewItem item = e.Source as TreeViewItem; if (item.Items.Count == 1 && item.Items[0] is string) { item.Items.Clear(); DirectoryInfo expandedDir = null; if (item.Tag is DriveInfo) expandedDir = (item.Tag as DriveInfo).RootDirectory; else if (item.Tag is DirectoryInfo) expandedDir = item.Tag as DirectoryInfo; try { foreach (DirectoryInfo subDir in expandedDir.GetDirectories()) { item.Items.Add(CreateTreeViewItem(subDir)); } } catch { } } } | cs |
댓글 없음:
댓글 쓰기