Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF TreeView HierarchicalDataTemplate - binding to object with multiple child collections
    primarykey
    data
    text
    <p>I am trying to get a <code>TreeView</code> to bind my collection so that all groups show nested groups and each group will show entry.</p> <p>How can I use the <code>HierarchicalDataTemplate</code> so that the <code>TreeView</code> will process both SubGroups and Entries collection?</p> <p>Groups show subgroups and entries:</p> <pre><code>Example: Group1 --Entry --Entry Group2 --Group4 ----Group1 ------Entry ------Entry ----Entry ----Entry --Entry --Entry Group3 --Entry --Entry </code></pre> <p><br></p> <h2>Objects:</h2> <hr /> <pre><code>namespace TaskManager.Domain { public class Entry { public int Key { get; set; } public string Name { get; set; } } } namespace TaskManager.Domain { public class Group { public int Key { get; set; } public string Name { get; set; } public IList&lt;Group&gt; SubGroups { get; set; } public IList&lt;Entry&gt; Entries { get; set; } } } </code></pre> <h2>Test data:</h2> <hr /> <pre><code>namespace DrillDownView { public class TestData { public IList&lt;Group&gt; Groups = new List&lt;Group&gt;(); public void Load() { Group grp1 = new Group() { Key = 1, Name = "Group 1", SubGroups = new List&lt;Group&gt;(), Entries = new List&lt;Entry&gt;() }; Group grp2 = new Group() { Key = 2, Name = "Group 2", SubGroups = new List&lt;Group&gt;(), Entries = new List&lt;Entry&gt;() }; Group grp3 = new Group() { Key = 3, Name = "Group 3", SubGroups = new List&lt;Group&gt;(), Entries = new List&lt;Entry&gt;() }; Group grp4 = new Group() { Key = 4, Name = "Group 4", SubGroups = new List&lt;Group&gt;(), Entries = new List&lt;Entry&gt;() }; //grp1 grp1.Entries.Add(new Entry() { Key=1, Name="Entry number 1" }); grp1.Entries.Add(new Entry() { Key=2, Name="Entry number 2" }); grp1.Entries.Add(new Entry() { Key=3,Name="Entry number 3" }); //grp2 grp2.Entries.Add(new Entry(){ Key=4, Name = "Entry number 4"}); grp2.Entries.Add(new Entry(){ Key=5, Name = "Entry number 5"}); grp2.Entries.Add(new Entry(){ Key=6, Name = "Entry number 6"}); //grp3 grp3.Entries.Add(new Entry(){ Key=7, Name = "Entry number 7"}); grp3.Entries.Add(new Entry(){ Key=8, Name = "Entry number 8"}); grp3.Entries.Add(new Entry(){ Key=9, Name = "Entry number 9"}); //grp4 grp4.Entries.Add(new Entry(){ Key=10, Name = "Entry number 10"}); grp4.Entries.Add(new Entry(){ Key=11, Name = "Entry number 11"}); grp4.Entries.Add(new Entry(){ Key=12, Name = "Entry number 12"}); grp4.SubGroups.Add(grp1); grp2.SubGroups.Add(grp4); Groups.Add(grp1); Groups.Add(grp2); Groups.Add(grp3); } } } </code></pre> <h2>XAML:</h2> <hr /> <pre><code>&lt;Window x:Class="DrillDownView.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TaskManager.Domain;assembly=TaskManager.Domain" Title="Window2" Height="300" Width="300"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TreeView Name="GroupView" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding}"&gt; &lt;TreeView.Resources&gt; &lt;HierarchicalDataTemplate DataType="{x:Type local:Group}" ItemsSource="{Binding SubGroups}"&gt; &lt;TextBlock Text="{Binding Path=Name}" /&gt; &lt;/HierarchicalDataTemplate&gt; &lt;HierarchicalDataTemplate DataType="{x:Type local:Entry}" ItemsSource="{Binding Entries}"&gt; &lt;TextBlock Text="{Binding Path=Name}" /&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/TreeView.Resources&gt; &lt;/TreeView&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <h2>XAML.CS:</h2> <hr /> <pre><code>public partial class Window2 : Window { public Window2() { InitializeComponent(); LoadView(); } private void LoadView() { TestData data = new TestData(); data.Load(); GroupView.ItemsSource = data.Groups; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload