Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Silverlight Hierarchy Tree Control

, 19 Oct 2010
A Hierarchy Tree with the ability to display any control as a node
HierarchyControl_scr.zip
HierarchyControl
Croll.Silverlight.Controls
Bin
Debug
Release
Croll.Silverlight.Controls.csproj.user
Properties
HierarchyControl
HierarchyControl.suo
HierarchyControl.Web
bin
ClientBin
HierarchyControl.xap
HierarchyControl.Web.csproj.user
Properties
Bin
Debug
HierarchyControl.csproj.user
Properties
View
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)

About the Author

RobCroll

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 19 Oct 2010
Article Copyright 2010 by RobCroll
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid