Click here to Skip to main content
15,896,111 members
Articles / Desktop Programming / WPF

WPF Control Factory

Rate me:
Please Sign up or sign in to vote.
4.25/5 (7 votes)
20 Apr 2010CPOL6 min read 38K   418   16  
This article explains some advantages and disadvantages of factories, and shows one to use for generating WPF Controls.
using System.Windows;
using System;
using Pfz.Collections;

namespace Pfz.WpfControls
{
	/// <summary>
	/// This class contains the dependency properties of the generic 
	/// PropertyBoundControl and some methods to discover if a DataSource
	/// is read-only.
	/// </summary>
	public static class BoundControl
	{
		#region DataSourceProperty
			/// <summary>
			/// The DataSource DependencyProperty.
			/// </summary>
			public static readonly DependencyProperty DataSourceProperty =
				DependencyProperty.RegisterAttached
				(
					"DataSource",
					typeof(object),
					typeof(BoundControl),
					new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, p_DataSourceChanged)
				);
			private static void p_DataSourceChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
			{
				IDataSourceChangedHandler handler = dependencyObject as IDataSourceChangedHandler;
				if (handler != null)
					handler.OnDataSourceChanged(args);
			}
			
			/// <summary>
			/// Gets the data-source of the given element.
			/// </summary>
			public static object GetDataSource(UIElement element)
			{
				return element.GetValue(DataSourceProperty);
			}
			
			/// <summary>
			/// Sets the data-source to the given element.
			/// </summary>
			public static void SetDataSource(UIElement element, object value)
			{
				element.SetValue(DataSourceProperty, value);
			}
		#endregion
		#region IsReadOnlyProperty
			/// <summary>
			/// The IsReadOnly DependencyProperty.
			/// </summary>
			public static readonly DependencyProperty IsReadOnlyProperty =
				DependencyProperty.RegisterAttached
				(
					"IsReadOnly",
					typeof(bool),
					typeof(BoundControl),
					new PropertyMetadata(false, p_IsReadOnlyChanged)
				);
			private static void p_IsReadOnlyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
			{
				IIsReadOnlyChangedHandler handler = dependencyObject as IIsReadOnlyChangedHandler;
				if (handler != null)
					handler.OnIsReadOnlyChanged(args);
			}
			
			/// <summary>
			/// Gets the IsReadOnly of the given element.
			/// </summary>
			public static bool GetIsReadOnly(UIElement element)
			{
				return (bool)element.GetValue(IsReadOnlyProperty);
			}
			
			/// <summary>
			/// Sets the IsReadOnly of the given element.
			/// </summary>
			public static void SetIsReadOnly(UIElement element, bool value)
			{
				element.SetValue(IsReadOnlyProperty, value);
			}
		#endregion
		
		
		#region IsDataSourceReadOnly
			/// <summary>
			/// Checks if the given data-source is read-only.
			/// Will use the registered functions for validation.
			/// </summary>
			public static bool IsDataSourceReadOnly(object dataSource)
			{
				if (dataSource == null)
					return true;
				
				var function = fIsReadOnlyFunctions.FindUpOrDefault(dataSource.GetType());
				if (function == null)
					return false;
				
				return function(dataSource);
			}
		#endregion
		#region RegisterIsReadOnlyFunction
			private static TypeDictionary<Func<object, bool>> fIsReadOnlyFunctions = new TypeDictionary<Func<object, bool>>();
		
			/// <summary>
			/// Registers a function to detect if a data-source is read-only.
			/// </summary>
			public static void RegisterIsReadOnlyFunction(Type objectType, Func<object, bool> function)
			{
				RegisterIsReadOnlyFunction(objectType, function, false);
			}

			/// <summary>
			/// Registers a function to detect if a data-source is read-only.
			/// </summary>
			public static void RegisterIsReadOnlyFunction(Type objectType, Func<object, bool> function, bool canUseForSubTypes)
			{
				fIsReadOnlyFunctions.Set(objectType, function, canUseForSubTypes);
			}
		#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
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