wpftreeviewhideroothierarchy

remove or hide root node in hierarchical TreeView


I have a TreeView like so:

<TreeView Name="tvObjects" Padding="0,5,0,0" Grid.Row="2">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
            <StackPanel Orientation="Horizontal" Margin="4,3" ContextMenu="{Binding Converter={StaticResource NodeTypeToContextMenuConverter}}">
                <Image Source="{Binding Converter={StaticResource StringToImageConverter}}" />
                <TextBlock Text="{Binding Title}" Padding="4,0,0,0" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

Its bound like so:

tvObjects.Items.Clear();
var root = new Node("SQLite", NodeType.SQLite);
// add db nodes, with sub nodes
tvObjects.Items.Add(root);

I would like to not show the root node, but only its children. Currently it looks like:

enter image description here

I want it to look like:

enter image description here

Note it has to be hierarchical.


Solution

  • If you don't want the root node, simply don't add it in Items collection. Add db nodes directly to Items collection:

    var node = new Node("main", NodeType.Whatever);
    // add sub nodes.
    tvObjects.Items.Add(node);
    
    var anotherNode = new Node("chin ook", NodeType.Whatever);
    // add sub nodes.
    tvObjects.Items.Add(anotherNode);