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;

namespace Croll.Silverlight.Controls
{
    public partial class HierarchyTree : UserControl
    {
        SolidColorBrush stroke;

        public HierarchyTree()
        {
            InitializeComponent();
            stroke = new SolidColorBrush(this.LineColor);
        }

        #region Properties

        //Items property
        public static DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(List<HierarchyNode>),
            typeof(HierarchyTree), new PropertyMetadata(new PropertyChangedCallback(ItemsChanged)));

        public List<HierarchyNode> Items
        {
            get
            {
                if (base.GetValue(ItemsProperty) == null)
                    base.SetValue(ItemsProperty, new List<HierarchyNode>());

                return (List<HierarchyNode>)base.GetValue(ItemsProperty);
            }

            set
            {
                base.SetValue(ItemsProperty, value);
            }
        }

        //LevelSpacing property
        public static DependencyProperty LevelSpacingProperty = DependencyProperty.Register("LevelSpacing", typeof(double),
            typeof(HierarchyTree), new PropertyMetadata(20d, new PropertyChangedCallback(LevelSpacingChanged)));

        public double LevelSpacing
        {
            get
            {
                return (double)base.GetValue(LevelSpacingProperty);
            }
            set
            {
                base.SetValue(LevelSpacingProperty, value);
            }
        }

        //ChildSpacing property
        public static DependencyProperty ChildSpacingProperty = DependencyProperty.Register("ChildSpacing", typeof(double),
            typeof(HierarchyTree), new PropertyMetadata(20d, new PropertyChangedCallback(ChildSpacingChanged)));

        public double ChildSpacing
        {
            get
            {
                return (double)base.GetValue(ChildSpacingProperty);
            }
            set
            {
                base.SetValue(ChildSpacingProperty, value);
            }
        }

        //LineColor property
        public static DependencyProperty LineColorProperty = DependencyProperty.Register("LineColor", typeof(Color),
            typeof(HierarchyTree), new PropertyMetadata((Color)Colors.Black, new PropertyChangedCallback(LineColorChanged)));

        public Color LineColor
        {
            get
            {
                return (Color)base.GetValue(LineColorProperty);
            }
            set
            {
                base.SetValue(LineColorProperty, value);
            }
        }


        #endregion Properties

        #region Events

        private static void ItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // Code for dealing with author name changes 
        }

        private static void LevelSpacingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // Code for dealing with author name changes 
        }

        private static void ChildSpacingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // Code for dealing with author name changes 
        }

        private static void LineColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HierarchyTree Tree = d as HierarchyTree;
            Color NewColor = (Color)e.NewValue;
            Tree.stroke.Color = NewColor;
        }

        #endregion Events



        public void Display()
        {
            double Top = 10;
            double Left = 20;

            LayoutRoot.Children.Clear();
            //int Level = 0;
            List<HierarchyNode> HierarchyItems = this.Items;


            for (int i = 0; i < HierarchyItems.Count; i++)
            {
                Left = AddControl(Top, Left, HierarchyItems[i], true);

                //Now build lines
            }

        }

        private double AddControl(double top, double left, HierarchyNode node, bool firstNode)
        {
            double ControlLeft = left;

            if (node.Children.Count > 0)
            {
                double LineTop = top + node.Control.ActualHeight + (LevelSpacing / 2);
                double ChildTop = top + node.Control.ActualHeight + LevelSpacing;
                double ChildLeft = left;

                //Add child controls to canvas first
                for (int i = 0; i < node.Children.Count; i++)
                {
                    ChildLeft = AddControl(ChildTop, ChildLeft, node.Children[i], false);
                }

                //Create join line
                double FirstNodePoint = node.Children.First().Control.Margin.Left + (node.Children.First().Control.ActualWidth / 2);
                double LastNodePoint = node.Children.Last().Control.Margin.Left + (node.Children.Last().Control.ActualWidth / 2);
                AddLine(FirstNodePoint, LastNodePoint, LineTop, LineTop);

                //Add line from join to parent
                double CenterPoint = FirstNodePoint + ((LastNodePoint - FirstNodePoint) / 2);
                ControlLeft = CenterPoint - (node.Control.ActualWidth / 2);

                left = ChildLeft + ChildSpacing;

            }
            else
            {
                left += node.Control.ActualWidth + ChildSpacing;
            }

            if (!firstNode)
                AddChildToParentLine(ControlLeft, top, node);

            if (node.Children.Count > 0)
                AddParentToChildLine(ControlLeft, top, node);

            //Add control to canvas
            node.Control.Margin = new Thickness(ControlLeft, top, 0, 0);
            LayoutRoot.Children.Add(node.Control);

            return left;
        }

        private void AddLine(double x1, double x2, double y1, double y2)
        {
            Line NewLine = new Line();
            NewLine.X1 = x1;
            NewLine.X2 = x2;
            NewLine.Y1 = y1;
            NewLine.Y2 = y2;
            NewLine.Stroke = stroke;
            LayoutRoot.Children.Add(NewLine);

        }

        private void AddChildToParentLine(double controlLeft, double top, HierarchyNode node)
        {
            double CentreControlLeft = controlLeft + (node.Control.ActualWidth / 2);
            AddLine(CentreControlLeft, CentreControlLeft, top - (LevelSpacing / 2), top);
        }

        private void AddParentToChildLine(double controlLeft, double top, HierarchyNode node)
        {
            double CentreControlLeft = controlLeft + (node.Control.ActualWidth / 2);
            double LineTop = top + node.Control.ActualHeight;
            AddLine(CentreControlLeft, CentreControlLeft, LineTop, LineTop + (LevelSpacing / 2));
        }


    }

    public class HierarchyNode
    {

        private FrameworkElement control;
        private List<HierarchyNode> children = new List<HierarchyNode>();

        public HierarchyNode(FrameworkElement control)
        {
            this.control = control;
        }

        public FrameworkElement Control
        {
            get { return this.control; }
            set { control = value; }
        }

        public List<HierarchyNode> Children
        {
            get { return this.children; }
            set { children = value; }
        }

        //public static HierarchyNode createButtonItem(string text)
        //{
        //    Button button = new Button() { Content = text };
        //    button.Width = 60;
        //    button.Height = 20;
        //    //button.Content = text;
        //    return new HierarchyNode(button);

        //}

    }
}

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
Web01 | 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