Click here to Skip to main content
15,881,967 members
Articles / Desktop Programming / WPF

A Graph Tree Drawing Control for WPF

Rate me:
Please Sign up or sign in to vote.
4.84/5 (34 votes)
23 Feb 2009CPOL10 min read 282.1K   9K   139  
This article describes and implements a graph drawing control for tree structures structured in a WPF panel.
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using GraphLayout;

namespace TreeContainer
{
	public class TreeConnectionsAdorner : Adorner
	{
		#region Private variables
		TreeContainer _tc;
		#endregion

		#region Constructors
		static TreeConnectionsAdorner()
		{
		}

		public TreeConnectionsAdorner(UIElement adornedElement)
			: base(adornedElement)
		{
			_tc = adornedElement as TreeContainer;
			IsHitTestVisible = false;
		}
		#endregion

		#region Point Conversion
		Point PtFromDPoint(DPoint dpt)
		{
			return new Point(dpt.X, dpt.Y);
		}
		#endregion

		#region Rendering
		protected override void OnRender(DrawingContext drawingContext)
		{
			List<TreeConnection> lsttcn = _tc.Connections;
			if (lsttcn != null)
			{
				SolidColorBrush brsh = new SolidColorBrush(Colors.Black);
				brsh.Opacity = 0.5;
				Pen pen = new Pen(brsh, 1.0);
				Point ptLast = new Point(0, 0);
				bool fHaveLastPoint = false;

				base.OnRender(drawingContext);
				foreach (TreeConnection tcn in lsttcn)
				{
					fHaveLastPoint = false;
					foreach (DPoint dpt in tcn.LstPt)
					{
						if (!fHaveLastPoint)
						{
							ptLast = PtFromDPoint(tcn.LstPt[0]);
							fHaveLastPoint = true;
							continue;
						}
						drawingContext.DrawLine(pen, PtFromDPoint(dpt), ptLast);
						ptLast = PtFromDPoint(dpt);
					}
				}
			}
		}
		#endregion
	}
}

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
Web Developer
United States United States
I've been doing programming for close to 30 years now. I started with C at Bell Labs and later went to work for Microsoft for many years. I currently am a partner in Suckerpunch LLC where we produce PlayStation games (www.suckerpunch.com).

I am mainly interested in AI, graphics and more mathematical stuff. Dealing with client/server architectures and business software doesn't do that much for me. I love math, computers, hiking, travel, reading, playing piano, fruit jars (yes - I said fruit jars) and photography.

Comments and Discussions