Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C# 4.0

Silverlight Hierarchy Tree Control

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
19 Oct 2010CPOL4 min read 53.2K   1.1K   32  
A Hierarchy Tree with the ability to display any control as a node
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Croll.Silverlight.Controls;

namespace HierarchyControl
{
    public partial class MainPage : UserControl
    {
        private string controlName = "Button";

        public MainPage()
        {
            InitializeComponent();
            populateHierarchy();
        }

        private void populateHierarchy()
        {
            FamilyTree.Nodes.Clear();

            HierarchyNode DavidNode = CreateNode("David");
            HierarchyNode JennyNode = CreateNode("Jenny");
            //HierarchyNode LucyNode = CreateNode("Lucy");
            HierarchyNode JohnNode = CreateNode("John");
            DavidNode.Children.Add(JennyNode);
            //DavidNode.Children.Add(LucyNode);
            DavidNode.Children.Add(JohnNode);
            
            //HierarchyNode DebbieNode = CreateNode("Debbie");
            //HierarchyNode HuonNode = CreateNode("Huon");
            //HierarchyNode JuliaNode = CreateNode("Julia");
            //LucyNode.Children.Add(DebbieNode);
            //LucyNode.Children.Add(HuonNode);
            //LucyNode.Children.Add(JuliaNode);
            
            //HierarchyNode RobertNode = CreateNode("Robert");
            //HierarchyNode Lucy2Node = CreateNode("Lucy");
            //HierarchyNode PeterNode = CreateNode("Peter");
            //RobertNode.Children.Add(Lucy2Node);
            //RobertNode.Children.Add(PeterNode);

            //HierarchyNode MeganNode = CreateNode("Megan");
            //HuonNode.Children.Add(RobertNode);
            //HuonNode.Children.Add(MeganNode);

            HierarchyNode JoeNode = CreateNode("Joe");
            HierarchyNode ZaneNode = CreateNode("Zane");
            HierarchyNode HugoNode = CreateNode("Hugo");
            JohnNode.Children.Add(JoeNode);
            JohnNode.Children.Add(ZaneNode);
            JohnNode.Children.Add(HugoNode);

            FamilyTree.Nodes.Add(DavidNode);

            FamilyTree.Display();

        }

        private HierarchyNode CreateNode(string text)
        {
            FrameworkElement Element;
            switch (controlName)
            {
                case "Button":
                    Element = new Button() { Content = text};
                    return new HierarchyNode(Element, 60, 20);

                case "TextBlock":
                    Element = new TextBlock() { Text = text, 
                        TextAlignment = TextAlignment.Center, 
                        Foreground = new SolidColorBrush(Colors.White) };
                    return new HierarchyNode(Element, 60, 20);

                case "MyUserControl":
                    Element = new MyUserControl(text, "2/2/2001");
                    return new HierarchyNode(Element, 120, 46);

                default:
                    Element = new Button() { Content = text };
                    return new HierarchyNode(Element, 60, 20);
            }
        }

        private void DisplayTextBlockButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.controlName != "TextBlock")
            {
                this.controlName = "TextBlock";
                FamilyTree.Nodes.Clear();
                populateHierarchy();
            }
        }

        private void DisplayButtonButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.controlName != "Button")
            {
                this.controlName = "Button";
                FamilyTree.Nodes.Clear();
                populateHierarchy();
            }
        }

        private void DisplayMyUserControlButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.controlName != "MyUserControl")
            {
                this.controlName = "MyUserControl";
                FamilyTree.Nodes.Clear();
                populateHierarchy();
            }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
Enjoying life developing mobile device software for Contractors Apps and Ezi App.

I also teach C#, Java and Project Management a couple of evenings a week.

Prior to moving to DCB, I'd been a Windows software developer for nearly 15 years

Comments and Discussions