ARTICLE

TreeView control in Silverlight using VB.NET

Posted by Rohatash Kumar Articles | Silverlight using VB.NET December 22, 2010
In this article we will learn how to use TreeView control in Silvelight 4.
 
Reader Level:

In this article we will learn how to use TreeView control in Silvelight 4.

TreeView control

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.

Property - Figure1 defines the property window.

tr1.gif

Figure 1.

Items Property - Initial collection of the nodes for the tree.

tr2.gif

Figure 2.

Now click on the Items property from the property window. This is the property window for the parent node. For child node click again Items property of collection editor window.

tr3.gif

Figure 3.

For example

Drag and drop a Treeview control on the form and set items collection property. The form looks like this.

tr4.gif

Figure 4.

Foreground and Background property

Now using background and foreground property of the TreeView control.

tr5.gif

Figure 5.

The form looks like this.

tr6.gif

Figure 6.

XAML code

<UserControl x:Class="SilverlightApplication46.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="404" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

 

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

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

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

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

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

                </sdk:TreeViewItem>

            </sdk:TreeViewItem>

            <sdk:TreeViewItem Header="Item2" Background="SteelBlue">

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

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

                </sdk:TreeViewItem>

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

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

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

                </sdk:TreeViewItem>

            </sdk:TreeViewItem>

            <sdk:TreeViewItem Header="Item3" Background="Wheat" Foreground="Brown">

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

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

                </sdk:TreeViewItem>

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

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

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

                </sdk:TreeViewItem>

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

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

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

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

                </sdk:TreeViewItem>

            </sdk:TreeViewItem>

        </sdk:TreeView>

    </Grid>

</UserControl>

 

Now save and run the application.


tr7.gif

Figure 7.

 

Now Expand all node. Then form looks like this.


tr8.gif

Figure 8.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor