//event handler for the Item Expanded event void ItemExpanded(object sender, RoutedEventArgs e) { TreeViewItem itemExpanded = (TreeViewItem)e.OriginalSource; //get the object being expanded DirectoryDisplayItem displayItem = (DirectoryDisplayItem)itemExpanded.Header; //check if the children of this item are dummy items if (displayItem.ContainsDummyItems) { //if the children are dummy children replace them with the actual data displayItem.ChildItems.Clear(); var modelData = FileSystemDataService.GetChildDirectories(displayItem.Path); if (modelData != null) { var children = from x in modelData select new DirectoryDisplayItem { Name = x.Name, Path = x.FullName }; //add all the data to the list foreach (var child in children) { displayItem.ChildItems.Add(child); } displayItem.ContainsDummyItems = false; } } }
//load all the files for the directory private void LoadFiles(DirectoryDisplayItem dir) { if (dir != null) { DataSource = FileSystemDataService.GetChildFiles(dir.Path); } }
/// <summary> /// Default constructor /// </summary> public DirectorySelectorController() { DataSource = (from x in FileSystemDataService.GetRootDirectories() select new DirectoryDisplayItem { Name = x.Name, Path = x.FullName }).ToList(); //Register to the SelectedIndex Routed event EventManager.RegisterClassHandler(typeof(Control), TreeViewItem.ExpandedEvent, (RoutedEventHandler)ItemExpanded); //Register to the SelectedIndex Routed event EventManager.RegisterClassHandler(typeof(Control), TreeView.SelectedItemChangedEvent, (RoutedEventHandler)ItemSelected); }