Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF TreeView hierarchical binding.

just starting with wpf. I need to bind the object (Hierarchical) Folder

public class Folder
{
    public Folder()
    {
        this.Name = string.Empty;
        this.Modules = new ObservableCollection<Module>();
        this.Folders = new List<Folder>();
        this.HasChild = false;
    }

    public Folder(Folder parent)
    {
        this.Name = string.Empty;
        this.Modules = new ObservableCollection<Module>();
        this.Folders = new List<Folder>();
        this.HasChild = false;
        this.Parent = parent;
    }

    public bool HasChild { get; set; }

    public string Name { get; set; }

    public List<Folder> Folders { get; set; }

    public ObservableCollection<Module> Modules { get; set; }

    public Folder Parent { get; set; }

    public Folder IfItemExists(string name)
    {
        foreach (Folder folder in Folders)
        {
            if (folder.Name == name)
            {
                return folder;
            }
        }

        return null;
    }
}

to the treeview. I am doing like that

   <TreeView Name="treeView" Margin="5">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=Folders}" DataType="{x:Type ModulesUpToDateChecker:Folder}">
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Name}"></TextBlock>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

and have just empty docpanel. The object is filled write. Eche child has own child with the same Type.

like image 931
Baranovskiy Dmitry Avatar asked Sep 21 '12 14:09

Baranovskiy Dmitry


2 Answers

A HierarchicalDataTemplate is already a DataTemplate (it derives from it). So just skip the ItemTemplate and DataTemplate stuff inside your HierarchicalDataTemplate like so:

<TreeView Name="treeView" Margin="5">
  <TreeView.Resources>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=Folders}" DataType="{x:Type WpfApplication220:Folder}">
      <TextBlock Text="{Binding Path=Name}"></TextBlock>
    </HierarchicalDataTemplate>
  </TreeView.Resources>
</TreeView>

Oh and you have to set the ItemsSource of your treeview either programmatically or in your markup ..

treeView.ItemsSource = ..yourFolderList..
like image 147
SvenG Avatar answered Oct 02 '22 07:10

SvenG


Try to do this:

 <TreeView Name="treeView" Margin="5">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Folders}" DataType="x:Type ModulesUpToDateChecker:Folder}">
           <Grid>
             <TextBlock Text="{Binding Path=Name}"></TextBlock>
           </Grid>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>
like image 42
Raúl Otaño Avatar answered Oct 02 '22 09:10

Raúl Otaño