2011-03-15 152 views
0

我想使樹視圖顯示文件系統。Treeview綁定wpf

public class FileSystem 
{ 
    public IList< Folder> folders; 

    public FileSystem() 
    { 
     foreach (DriveInfo di in DriveInfo.GetDrives()) 
     { 
      Folder f = new Folder(di.Name); 
      f.fillSubFolders(); 
      folders.Add(f); 
     } 
    } 
} 

public class FileItem 
{ 
    public string name; 
    public FileItem(string _name) 
    { 
     name = _name; 
    } 
} 

public class Folder 
{ 
    public string name; 
    public IList<Folder> subFolders; 
    public IList<FileItem> items; 

    public Folder(string _name) 
    { 
     name = _name; 
     subFolders = new List<Folder>(); 
     items = new List<FileItem>(); 
    } 

    public void fillSubFolders() { 
     foreach (string fl in Directory.GetFiles(name)) 
     { 
      FileItem f = new FileItem(fl); 
      items.Add(f); 
     } 
     foreach (string dir in Directory.GetDirectories(name)) 
     { 
      Folder f = new Folder(dir); 
      subFolders.Add(f); 
      f.fillSubFolders(); 
     } 
    } 
} 

我應該在XAML代碼中添加什麼來綁定數據?

<TreeView Height="311" HorizontalAlignment="Left" Name="treeView1" VerticalAlignment="Top" Width="199" ItemsSource="{Binding items}"> 
     <TreeView.Resources> 
      <HierarchicalDataTemplate DataType="{Binding}"> 

      </HierarchicalDataTemplate> 
     </TreeView.Resources> 
    </TreeView> 

回答

1

你可能想看看this article(尤其是 「View實現」 一節)。