Kind of an odd problem I have here.
I have a pretty basic recursive tree structure:
public class TreeNode
{
public string Name { get; set; }
public IEnumerable<TreeNode> Children { get; set; }
}
and am displaying the data in a TreeView using a HierarchicalDataTemplate, like so:
<TreeView Name="_tree">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" >
<CheckBox/>
<TextBlock Text="{Binding Path=Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
I populate the tree from code behind:
//allTreeNodes is a list of all existing Tree objects
public void PopulateTree(List<TreeNode> allTreeNodes)
{
foreach (var node in allTreeNodes)
{
_tree.Items.Add(node);
}
}
The result is a TreeView with each existing Tree object as a root node with their respective subtrees.
Note that each TreeNode can be displayed in the tree at multiple locations depending on whether or not it has ancestors.
Up to this point, everything works well, but what I want to do now is only have the checkboxes displayed for the visual root nodes only. I've attempted using a DataTemplateSelector with two HierarchicalDataTemplates, selecting the Template based on an additional boolean property of the TreeNode, but this doesn't work because the TreeNode needs to be displayed with the checkbox once and potentially multiple times without the checkbox.
Any help is appreciated.
Edit: Here is some dummy data to help explain what I want. (Also note that there are no cyclic references in the data.)
+TreeNodeA
-TreeNodeB
-TreeNodeC
-TreeNodeB
+TreeNodeB
+TreeNodeC
-TreeNodeB
+TreeNodeD
-TreeNodeE
-TreeNodeC
-TreeNodeB
+TreeNodeE
-TreeNodeC
-TreeNodeB
In the above view, only the nodes preceded with +
should have checkboxes.