Click here to Skip to main content
11,435,407 members (50,575 online)
Click here to Skip to main content
Add your own
alternative version

WPF Control Factory

, 20 Apr 2010 CPOL
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.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)

Share

About the Author

Paulo Zemek
Engineer Microsoft Corporation
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.

Now I just started working as a Senior Software Engineer at Microsoft.

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
Microsoft MVP 2013-2014

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