Treeview绑定wpf

时间:2011-03-15 10:55:50

标签: wpf binding treeview

我想制作一个显示文件系统的树视图。

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>

2 个答案:

答案 0 :(得分:1)

您可能需要查看this article(尤其是“查看实施”部分)。

答案 1 :(得分:0)

以下链接可以提供帮助

http://www.mattlong.com.au/?p=37