<Window X: class = "wpffolderexplorer. mainwindow "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: My =" CLR-namespace: wpffolderexplorer "Title =" folder Explorer "Height =" 350 "width =" 525 "> <window. resources> <! -- Objectdataprovider: wraps and creates an object that can be used as the binding source. --> <Objectdataprovider X: Key = "rootfolderdataprovider"> <! -- Objectinstance: object instance --> <objectdataprovider. objectinstance> <! -- Fullpath: an attribute of the folder class --> <my: folder fullpath = "C: \ Program Files \"/> </objectdataprovider. objectinstance> </objectdataprovider> <! -- Hierarchicaldatatemplate: hierarchical data template -->
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Collections.ObjectModel;namespace WPFFolderExplorer{ public class Folder { private DirectoryInfo _folder; private ObservableCollection<Folder> _subFolders; private ObservableCollection<FileInfo> _files; public Folder() { this.FullPath = @"c:\Program Files\"; } public string Name { get { return this._folder.Name; } } public string FullPath { get { return this._folder.FullName; } set { if (Directory.Exists(value)) { this._folder = new DirectoryInfo(value); } else { throw new ArgumentException("must exist", "fullPath"); } } } public ObservableCollection<FileInfo> Files { get { if (this._files == null) { this._files = new ObservableCollection<FileInfo>(); FileInfo[] fi = this._folder.GetFiles(); for (int i = 0; i < fi.Length; i++) { this._files.Add(fi[i]); } } return this._files; } } public ObservableCollection<Folder> SubFolders { get { if (this._subFolders == null) { this._subFolders = new ObservableCollection<Folder>(); DirectoryInfo[] di = this._folder.GetDirectories(); for (int i = 0; i < di.Length; i++) { Folder newFolder = new Folder(); newFolder.FullPath = di[i].FullName; this._subFolders.Add(newFolder); } } return this._subFolders; } } }}