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

WPF Control Factory

, 20 Apr 2010
This article explains some advantages and disadvantages of factories, and shows one to use for generating WPF Controls.
WpfControlsAndSample.zip
WpfControlsAndSample
Pfz.ClassLibraries
Pfz
Caching
Collections
DataTypes
Extensions
Pfz.snk
Properties
Ranges
Threading
Pfz.WpfControls
BoundGridParts
Extensions
Pfz.WpfControls.snk
Properties
TypeConverters
WpfControlFactorySample
bin
Debug
Pfz.dll
Pfz.WpfControls.dll
WpfControlFactorySample.exe
Properties
Settings.settings
WpfControlFactorySample.suo
using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Windows;
using Pfz.WpfControls;

namespace Pfz.WpfControls
{
	/// <summary>
	/// Converts grid property bindings to and from strings.
	/// </summary>
	public class GridPropertyBindingTypeConverter:
		TypeConverter
	{
		/// <summary>
		/// Returns true for strings.
		/// </summary>
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			return sourceType == typeof(string);
		}

		/// <summary>
		/// Returns true for strings.
		/// </summary>
		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			return destinationType == typeof(string);
		}

		/// <summary>
		/// Converts a string into a property binding.
		/// The full format is:
		/// FullTypeName.PropertyName,Width=Display name.
		/// </summary>
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			string strValue = value as string;
			return ConvertFrom(strValue);
		}

		/// <summary>
		/// Converts a string into a property binding.
		/// The full format is:
		/// FullTypeName.PropertyName,Width=Display name.
		/// </summary>
		public static GridPropertyBinding ConvertFrom(string value)
		{
			if (string.IsNullOrEmpty(value))
				return null;

			int pos = value.IndexOf('=');
			string propertyName;
			string displayName = null;

			if (pos == -1)
				propertyName = value;
			else
			{
				propertyName = value.Substring(0, pos);
				displayName = value.Substring(pos + 1);
			}
			
			GridLength width = new GridLength();
			pos = propertyName.IndexOf(',');
			if (pos != -1)
			{
				string lengthString = propertyName.Substring(pos+1);
				propertyName = propertyName.Substring(0, pos);
				
				GridUnitType unitType = GridUnitType.Pixel;
				if (lengthString.EndsWith("*"))
				{
					unitType = GridUnitType.Star;
					lengthString = lengthString.Substring(0, lengthString.Length-1);
				}
				
				double lengthValue = 1;
				if (!string.IsNullOrEmpty(lengthString))
					lengthValue = double.Parse(lengthString, CultureInfo.InvariantCulture);
					
				width = new GridLength(lengthValue, unitType);
			}

			PropertyInfo propertyInfo = PropertyInfoTypeConverter.ConvertFrom(propertyName);
			return new GridPropertyBinding { DisplayName = displayName, Property = propertyInfo, Width=width };
		}
		
		/// <summary>
		/// Converts a grid property binding to a string.
		/// </summary>
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			var propertyBound = value as GridPropertyBinding;
			return ConvertTo(propertyBound);
		}

		/// <summary>
		/// Converts a grid property binding to a string.
		/// </summary>
		public static string ConvertTo(GridPropertyBinding propertyBinding)
		{
			if (propertyBinding == null)
				return null;
				
			StringBuilder result = new StringBuilder();
			
			var property = propertyBinding.Property;
			string propertyFullName = PropertyInfoTypeConverter.ConvertTo(property);
			result.Append(propertyFullName);
			
			var width = propertyBinding.Width;
			if (!width.IsAuto)
			{
				result.Append(',');
				result.Append(width.Value);
				
				if (width.IsStar)
					result.Append('*');
			}
			
			var displayName = propertyBinding.DisplayName;
			if (displayName != null)
			{
				result.Append('=');
				result.Append(displayName);
			}
			
			return result.ToString();
		}
	}
}

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)

Share

About the Author

Paulo Zemek
Engineer
United States United States
I started to program computers when I was 11 years old, as a hobbist, 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 they 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
Microsoft MVP 2013-2014

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 20 Apr 2010
Article Copyright 2010 by Paulo Zemek
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid