Skip to content

Instantly share code, notes, and snippets.

@jsonw23
Created February 27, 2012 20:39
Show Gist options
  • Save jsonw23/1926882 to your computer and use it in GitHub Desktop.
Save jsonw23/1926882 to your computer and use it in GitHub Desktop.
Folder Tree WPF Control: Part 1 - View Model
namespace GeekJ.FolderTreeControl.Model
{
public class Drive : FolderTreeItem
{
private DriveInfo _driveInfo;
public DriveInfo DriveInfo
{
get
{
return _driveInfo;
}
set
{
_driveInfo = value;
}
}
public Drive(DriveInfo driveInfo)
{
DriveInfo = driveInfo;
}
public string Label
{
get
{
return DriveInfo.Name;
}
}
private ObservableCollection<Folder> _folders;
public ObservableCollection<Folder> Folders
{
get
{
if (_folders == null)
{
_folders = new ObservableCollection<Folder>();
_folders.Add(new Folder());
}
return _folders;
}
}
public override void LoadChildren()
{
Folders.Clear();
foreach (var child in Folder.LoadSubFolders(DriveInfo.RootDirectory))
{
Folders.Add(child);
}
}
}
}
namespace GeekJ.FolderTreeControl.Model
{
public class Folder : FolderTreeItem
{
private System.IO.DirectoryInfo directoryInfo;
public Folder()
{
}
public Folder(System.IO.DirectoryInfo directoryInfo)
{
this.directoryInfo = directoryInfo;
}
private ObservableCollection<Folder> _folders;
public ObservableCollection<Folder> Folders
{
get
{
if (_folders == null)
{
_folders = new ObservableCollection<Folder>();
_folders.Add(new Folder());
}
return _folders;
}
}
public string Label
{
get
{
return directoryInfo.Name;
}
}
public override void LoadChildren()
{
Folders.Clear();
foreach (var child in Folder.LoadSubFolders(directoryInfo))
{
Folders.Add(child);
}
}
internal static IEnumerable<Folder> LoadSubFolders(System.IO.DirectoryInfo directoryInfo)
{
return directoryInfo.EnumerateDirectories().Select(x => new Folder(x));
}
}
}
namespace GeekJ.FolderTreeControl.Model
{
public class FolderTreeViewModel : ViewModelBase
{
private ObservableCollection<Drive> _drives;
public ObservableCollection<Drive> Drives
{
get { return _drives; }
set
{
_drives = value;
OnPropertyChanged("Drives");
}
}
public FolderTreeViewModel()
{
_drives = new ObservableCollection<Drive>();
foreach (var driveInfo in DriveInfo.GetDrives())
{
_drives.Add(new Drive(driveInfo));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment