WPF TreeView hierarchical binding.

前端 未结 2 1262
一个人的身影
一个人的身影 2021-01-01 03:01

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

public class Folder
{
    public Folder()
    {
        this.Name = string.Empty;
          


        
相关标签:
2条回答
  • 2021-01-01 03:05

    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>
    
    0 讨论(0)
  • 2021-01-01 03:17

    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..
    
    0 讨论(0)
提交回复
热议问题