Click here to Skip to main content
15,891,708 members
Articles / Desktop Programming / WPF

Templates, Inversion of Control, Factories, and so on

Rate me:
Please Sign up or sign in to vote.
4.75/5 (5 votes)
8 Jul 2011CPOL11 min read 23K   270   18  
This article gives a little presentation of Control Templates, Data Templates, Inversion of Control, and Factories, explaining why they are all related and how to better use them.
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Pfz.WpfControls
{
	/// <summary>
	/// This grid considers columns widths (Auto is considered *), but also puts each element in the next cell automatically,
	/// so you don't need to create many row definitions or to set Grid.Column and Grid.Row properties.
	/// </summary>
	public sealed class WrapGrid:
		Panel
	{
		/// <summary>
		/// Create a new WrapGrid instance.
		/// </summary>
		public WrapGrid()
		{
			_widths.CollectionChanged += (a, b) => InvalidateMeasure();
		}

		private ObservableCollection<GridLength> _widths = new ObservableCollection<GridLength>();

		/// <summary>
		/// Gets the Widths collection.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		public ObservableCollection<GridLength> ColumnWidths
		{
			get
			{
				return _widths;
			}
		}

		private double _rowHeight;
		/// <summary>
		/// Gets or sets the height of the rows.
		/// 0 means auto. Negative values is like putting * in all rows.
		/// </summary>
		public double RowHeight
		{
			get
			{
				return _rowHeight;
			}
			set
			{
				if (value == _rowHeight)
					return;

				_rowHeight = value;
				InvalidateMeasure();
			}
		}

		/// <summary>
		/// Measures the size of each child and its own.
		/// </summary>
		protected override Size MeasureOverride(Size availableSize)
		{
			if (Children.Count == 0)
				return base.MeasureOverride(availableSize);

			if (_rowHeight < 0)
				return availableSize;

			int columnCount = _widths.Count;
			int rowCount = 1;
			if (columnCount > 0)
				rowCount = (Children.Count + columnCount - 1) / columnCount;

			if (_rowHeight > 0)
				return new Size(availableSize.Width, rowCount * _rowHeight);

			double maxWidth = 0;
			double maxHeight = 0;
			if (columnCount == 0)
			{
				Size infiniteSize = new Size(availableSize.Width / Children.Count, availableSize.Height);
				foreach(UIElement child in Children)
				{
					child.Measure(infiniteSize);

					var desiredSize = child.DesiredSize;
					maxHeight = Math.Max(desiredSize.Height, maxHeight);
					maxWidth += desiredSize.Width;
				}
			}
			else
			{
				double rowWidth = 0;
				double[] widths;
				double[] lefts;
				_GetWidths(availableSize, columnCount, out widths, out lefts);
				int columnIndex = 0;
				foreach(UIElement child in Children)
				{
					child.Measure(new Size(widths[columnIndex], availableSize.Height));
					var desiredSize = child.DesiredSize;
					maxHeight = Math.Max(desiredSize.Height, maxHeight);
					rowWidth += desiredSize.Width;
					
					columnIndex++;
					if (columnIndex == columnCount)
					{
						columnIndex = 0;
						rowWidth = 0;
						maxWidth = Math.Max(maxWidth, rowWidth);
					}
				}
			}

			return new Size(maxWidth, maxHeight * rowCount);
		}

		/// <summary>
		/// Positions the children.
		/// </summary>
		protected override Size ArrangeOverride(Size finalSize)
		{
			var children = Children;
			int childrenCount = children.Count;

			if (childrenCount == 0)
				return base.ArrangeOverride(finalSize);

			int columnCount = _widths.Count;

			if (columnCount == 0)
			{
				double lastLeft = 0;
				double width = finalSize.Width/childrenCount;
				foreach(UIElement child in children)
				{
					child.Arrange(new Rect(lastLeft, 0, width, finalSize.Height));
					lastLeft += width;
				}
			}
			else
			{
				double[] widths;
				double[] lefts;
				_GetWidths(finalSize, columnCount, out widths, out lefts);

				double height = RowHeight;
				if (height <= 0)
				{
					if (height < 0)
					{
						int totalLines = (childrenCount + columnCount - 1) / columnCount;
						height = finalSize.Height / totalLines;
					}
					else
					{
						int columnIndex3 = 0;
						foreach(UIElement child in children)
						{
							child.Measure(new Size(widths[columnIndex3], finalSize.Height));
							height = Math.Max(child.DesiredSize.Height, height);

							columnIndex3 ++;
							if (columnIndex3 == columnCount)
								columnIndex3 = 0;
						}
					}
				}

				int columnIndex2 = 0;
				double lineTop = 0;
				foreach(UIElement child in children)
				{
					Rect rect = new Rect(lefts[columnIndex2], lineTop, widths[columnIndex2], height);

					child.Arrange(rect);

					columnIndex2 ++;
					if (columnIndex2 == columnCount)
					{
						columnIndex2 = 0;
						lineTop += height;
					}
				}
			}

			return base.ArrangeOverride(finalSize);
		}

		private void _GetWidths(Size finalSize, int columnCount, out double[] widths, out double[] lefts)
		{
			double availableWidth = finalSize.Width;
			double totalCalculated = 0;
			foreach (var width in _widths)
			{
				if (width.IsAbsolute)
					availableWidth -= width.Value;
				else
					totalCalculated += width.Value;
			}

			if (availableWidth < 0)
				availableWidth = 0;

			widths = new double[columnCount];
			lefts = new double[columnCount];
			double left = 0;
			for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
			{
				var columnWidth = _widths[columnIndex];

				double width;
				if (columnWidth.IsAbsolute)
					width = columnWidth.Value;
				else
					width = availableWidth * columnWidth.Value / totalCalculated;

				widths[columnIndex] = width;
				lefts[columnIndex] = left;

				left += width;
			}
		}
	}
}

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
Software Developer (Senior) Microsoft
United States United States
I started to program computers when I was 11 years old, as a hobbyist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.

At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do their work easier, faster and with less errors.

Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com

Codeproject MVP 2012, 2015 & 2016
Microsoft MVP 2013-2014 (in October 2014 I started working at Microsoft, so I can't be a Microsoft MVP anymore).

Comments and Discussions