Silverlight Tree View Control Example: Part 1

In this article we will be seeing how to create Silverlight TreeView control.

Xaml:

<sdk:TreeView/>

Steps Involved:

Creating a Silverlight Application:

  1. Open Visual Studio 2010.

  2. Go to File => New => Project.

  3. Select Silverlight from the Installed templates and choose the Silverlight Application template.

  4. Enter the Name and choose the location.

  5. Click OK.

  6. In the New Silverlight Application wizard check the "Host the Silverlight Application in a new Web site".

  7. Click OK.

Creating the UI:

Open MainPage.xaml file and replace the code with the following one.

  1. <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk

  2. x:Class="TreeViewControl.MainPage"  

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

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

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

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

  7. d:DesignHeight="300" d:DesignWidth="400">

  8. <Grid x:Name="LayoutRoot" Background="Gray">

  9. <sdk:TreeView  x:Name="Menu" Height="250" Width="250">

  10. <sdk:TreeViewItem Header="Menu">

  11. <sdk:TreeViewItem Header="SubMenu1">

  12. <sdk:TreeViewItem Header="Item1"/>

  13. sdk:TreeViewItem Header="Item2"/>

  14. <sdk:TreeViewItem Header="Item3"/>

  15. </sdk:TreeViewItem>

  16. <sdk:TreeViewItem Header="SubMenu2">

  17. <sdk:TreeViewItem Header="Item1"/>

  18. <sdk:TreeViewItem Header="Item2"/>

  19. <sdk:TreeViewItem Header="Item3"/>

  20. </sdk:TreeViewItem>

  21. </sdk:TreeViewItem>

  22. </sdk:TreeView>

  23. </Grid>

  24. </UserControl>

Hit F5.

Output:


Recent Posts

See All