voula_d
Topic Author
Posts: 17
Joined: 16 Feb 2023, 17:40

Hierarchical Treeview

14 Mar 2024, 11:40

Hi all,
I am trying to create a hierarchical treeview using a HierarchicalDataTemplate and a grouped list. The list is of type List<IGrouping<string, MyClass>> and the way I am binding the treeview to that list is the following:
<TreeView ItemsSource="{Binding GroupedList}" >
        <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding}">
            <TextBlock Text="{Binding Key}" Margin="3 2" />
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <ContentControl>
                        <TextBlock Text="{Binding ItemName}"></TextBlock>
                    </ContentControl>
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>  
    	</TreeView.ItemTemplate>
    </TreeView>
But this is not working, I am only getting the first level in the Tree and there is no expander or children displayed. Is this supported? If not, how should I bind hierarchical data to a treeview?
Thanks!
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: Hierarchical Treeview

27 Mar 2024, 11:31

Hi,

IGrouping is not supported by Noesis, to use a HierarchicalDataTemplate the list should contain the groups, and each group should contain a list of items that you can bind in the template ItemsSource:
public class ViewModel
{
  public List<Group> GroupedList { get; }
}

public class Group
{
  public string Key { get; set; }
  public List<MyClass> ItemsList { get; }
}

public class MyClass
{
  public string ItemName { get; set; }
}
<TreeView ItemsSource="{Binding GroupedList}" >
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding ItemsList}">
      <TextBlock Text="{Binding Key}" Margin="3,2" />
      <HierarchicalDataTemplate.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding ItemName}"/>
        </DataTemplate>
      </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests