TreeView control WPF in VB.NET

This article defines hierarchical or parent/child relationship view of data using treeview control.
  • 5515

TreeView control

TreeView control can be used for any data that can be represented in a hierarchy. The tree view provides an object oriented hierarchical or parent/child relationship view of data and meta-data. The most common example of a tree view is Windows Explorer's directory structure where disk drives contain folders, folders contain sub-folders and folders contain files.


Items Property - Items property is used to Initial collection of the nodes for the tree.

Foreground and Background property

The Background and Foreground property represents the background and foreground color of the TreeView control.

Now using Items property of the TreeView control to initial collection of the nodes.

XAML code

<Window x:Class="MainWindow"



    Title="MainWindow" Height="350" Width="525">

    <Grid x:Name="LayoutRoot" Background="White" Height="412">

        <TreeView Height="400" HorizontalAlignment="Left" Margin="30,9,0,0" Name="TreeView1" VerticalAlignment="Top" Width="318" Background="SpringGreen">

            <TreeViewItem Header="Item1" Name="TreeViewItem2" Foreground="Blue" Background="Aqua">

                <TreeViewItem Header="Sub Item1" Foreground="Brown" Background="Tan">

                    <TreeViewItem Header="Example1" Foreground="DarkRed" Background="Teal" />



            <TreeViewItem Header="Item2" Background="SteelBlue" Name="TreeViewItem4">

                <TreeViewItem Header="Sub Item1" Background="Yellow">

                    <TreeViewItem Header="Example1" Background="Yellow" />


                <TreeViewItem Header="Sub Item2" Background="Crimson">

                    <TreeViewItem Header="Example1" Background="Teal" />

                    <TreeViewItem Header="Example2" Background="Plum" />



            <TreeViewItem Header="Item3" Background="Wheat" Foreground="Brown" Name="TreeViewItem5">

                <TreeViewItem Header="Sub Item1" Background="YellowGreen">

                    <TreeViewItem Header="Example1" Background="Magenta" />


                <TreeViewItem Header="Sub Item2" Name="TreeViewItem1" Background="Red">

                    <TreeViewItem Header="Example1" Background="Purple" />

                    <TreeViewItem Header="Example2" Background="CadetBlue" Foreground="Blue" />


                <TreeViewItem Header="Sub Item3" Name="TreeViewItem3" Background="Yellow" Foreground="DarkGreen">

                    <TreeViewItem Header="Example1" Background="Indigo" Foreground="DarkRed" />

                    <TreeViewItem Header="Example2" Background="Lime" />

                    <TreeViewItem Header="Example3" Background="DarkBlue" />






Now run the application.



Now expand all the nodes.




More Articles

© 2020 DotNetHeaven. All rights reserved.