Click here to Skip to main content
15,891,184 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 54K   1.1K   32  
A Hierarchy Tree with the ability to display any control as a node
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace Croll.Silverlight.Controls
{
    /// <summary>
    /// Represents a node in the HierarchyTree NodeControl
    /// </summary>
    /// <remarks>This class is a prime candidate to inherit the PresentationFrameworkCollection<T> class 
    /// to allow developers to interact with the HierarchyTree Nodes in the Properties window in the same way nodes 
    /// can be added to a TreeView.  Unfortunately due to few abstract internal methods, this is not possible
    /// </remarks>
    public class HierarchyNode
    {

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

        /// <summary>
        /// Creates an instance of the HierarchyNode class and sets the controls width and height,
        /// </summary>
        /// <remarks>
        /// Due to the asyncronous nature of Silverlight the actual width and height of the NodeControl
        /// is not known until the HierarchyTree is displayed
        /// </remarks>
        /// <param name="NodeControl">Control to display in HierarchyTree</param>
        /// <param name="width">Width of NodeControl to display</param>
        /// <param name="height">Height of NodeControl to display</param>
        public HierarchyNode(FrameworkElement control, double width, double height)
        {
            this.control = control;
            control.Width = width;
            control.Height = height;
        }

        /// <summary>
        /// Control used to visually represent the node
        /// </summary>
        public FrameworkElement Control
        {
            get { return this.control; }
            set { control = value; }
        }

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

    }
}

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