Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have list of items this list have another list of items how to bind it to the TreeView
public List<JobesGroup> JobeGroups { get; set; } = new List<JobesGroup>()
   {
      new JobesGroup()
      {
          ID=1,
          GroupName="test1",
          Jobes=new List<Jobe>()
          {
              new Jobe()
              {
                  ID=1,
                  JobeName="sss"
              },
              new Jobe()
              {
                  ID=2,
                  JobeName="aaa"
              }
          }
      },
      new JobesGroup()
      {
          ID=2,
          GroupName="test2",
          Jobes=new List<Jobe>()
          {
              new Jobe()
              {
                  ID=3,
                  JobeName="ddd"
              },
              new Jobe()
              {
                  ID=4,
                  JobeName="fff"
              }
          }
      }
   };


i need the treeview main item is the GroupName and the subitemes is JobeName

What I have tried:

<TreeView HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <HierarchicalDataTemplate DataType="{x:Type vs:JobesGroup}" ItemsSource="{Binding JobeGroups}">
                        <TextBlock Text="{Binding GroupName}" />
                    </HierarchicalDataTemplate>
                    <DataTemplate DataType="{x:Type vs:Jobe}">
                        <TextBlock Text="{Binding JobeName}" />
                    </DataTemplate>
                </TreeView>


and

<TreeView HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding JobeGroups}">
                    <HierarchicalDataTemplate DataType="{x:Type vs:JobesGroup}" ItemsSource="{Binding JobeGroups}">
                        <TextBlock Text="{Binding GroupName}" />
                    </HierarchicalDataTemplate>
                    <DataTemplate DataType="{x:Type vs:Jobe}">
                        <TextBlock Text="{Binding JobeName}" />
                    </DataTemplate>
                </TreeView>


and

<TreeView HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding JobeGroups}">
                    <HierarchicalDataTemplate >
                        <TextBlock Text="{Binding GroupName}" />
                    </HierarchicalDataTemplate>
                    <DataTemplate DataType="{x:Type vs:Jobe}">
                        <TextBlock Text="{Binding JobeName}" />
                    </DataTemplate>
                </TreeView>
Posted
Updated 3-Mar-22 2:54am

 
Share this answer
 
Comments
Maciej Los 3-Mar-22 6:34am    
5ed!
Member 7912784 4-Mar-22 12:20pm    
Thank you
Here is solution thanks to thatguy at Stack Overflow - Where Developers Learn, Share, & Build Careers[^]

XML
<TreeView ItemsSource="{Binding JobeGroups}">
   <TreeView.ItemTemplate>
      <HierarchicalDataTemplate DataType="{x:Type local:JobesGroup}"
                                ItemsSource="{Binding Jobes}">
         <TextBlock Text="{Binding GroupName}"/>
         <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate DataType="{x:Type local:Jobe}">
               <TextBlock Text="{Binding JobeName}"/>
            </DataTemplate>
         </HierarchicalDataTemplate.ItemTemplate>
      </HierarchicalDataTemplate>
   </TreeView.ItemTemplate>
</TreeView>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900