jsomers89
jsomers89

Reputation: 173

Generate A treeview with a list of objects

The two classes I am using are as follows

public class FoundDirectory 
{
     public string fullPath { get; set; }
     public string parent { get; set; }
     public string directoryName { get; set; }
     public ObservableCollection<FoundDirectory> subDirectories { get; set; }
     public ObservableCollection<FoundFile> subFiles { get; set; }

     public override string ToString()
     {
         return directoryName;
     }
}

public class FoundFile 
{
    public string fileName { get; set; }
    public string fullPath { get; set; }

    public long fileLength { get; set; }

    public override string ToString()
    {
        return fileName;
    }
}

I am trying to generate a TreeView from a recursive hard drive scan. I pass in an ObservableCollection to the TreeView. I can get it to display the subdirectories nested correctly but can not get it to display the sub files at all. My XAML is as follows:

    <TreeView Name="directoryTree" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding Path=sortedDirList}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=subDirectories}">
                <StackPanel>
                    <TextBlock Text="{Binding Path=directoryName}"/>
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

I have tried multiple different ways to display the subfiles but nothing seems to work

Upvotes: 1

Views: 215

Answers (1)

Gordon Allocman
Gordon Allocman

Reputation: 778

Here is one way you could go about this:

public class Node
{
    public string FullPath { get; set; }
    public string Name { get; set;}
    public int Size {get;set;}
    public ObservableCollection<Node> Children {get; set;}

    public string ToString()
    {
        return Name;
    }
}

Xaml:

<TreeView Name="directoryTree" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding Path=sortedDirList}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
                <StackPanel>
                    <TextBlock Text="{Binding Path=Name}"/>
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

Then just make sortedDirList a list containing just the root directory, and that should expand out properly.

Note: The class/variable names are simply placeholders, feel free to rename them.

To recursively populate the tree write something like this should work:

public void DirectorySearch(Node node)
{
    foreach(string f in Directory.GetFiles(node.Name))
    {
        //initialize a node with the file info
        node.Children.Add(fileNode);
    }
    foreach(var d in Directory.GetDirectories(node.Name)
    {
        //initialize a node with directory info
        node.Children.Add(dirNode);
        DirectorySearch(dirNode);
    }
}

Upvotes: 1

Related Questions