Click here to Skip to main content
15,895,709 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.Linq;
using System.Windows;
using System.Windows.Controls;
using Pfz.Factoring;
using Pfz.WpfControls;
using System.Windows.Media;

namespace Pfz.Databasing.WpfControls
{
	/// <summary>
	/// Base lookup control. Use it only if you need a way to find
	/// LookupControls without knowing their specific generic 
	/// parameter type.
	/// </summary>
	public abstract class LookupControlBase:
		UserControl,
		IValueControl,
		IHasValueChanged,
		IHasDisplayName
	{
		internal KeyBoundControl _keyControl = new KeyBoundControl();
		internal TextBox _label = new TextBox { IsReadOnly = true, VerticalAlignment = VerticalAlignment.Bottom };

		private MenuItem _menuItemSearch;
		private MenuItem _menuItemCreate;
		internal MenuItem _menuItemEdit;
		private Button _button = new Button();
		
		/// <summary>
		/// Creates a new LookupControl instance.
		/// </summary>
		public LookupControlBase()
		{
			DockPanel.SetDock(_keyControl, Dock.Left);
			
			var panel = new DockPanel();
			panel.LastChildFill = true;
			var children = panel.Children;
			
			children.Add(_keyControl);
			children.Add(_label);
			
			var menuItemSearch = new MenuItem();
			_menuItemSearch = menuItemSearch;
			menuItemSearch.Header = "Search";
			menuItemSearch.Click +=	(sender, args) => Search();
			menuItemSearch.IsEnabled = CanSearch();

			bool canEdit = CanEdit();
			var menuItemCreateNew = new MenuItem();
			_menuItemCreate = menuItemCreateNew;
			menuItemCreateNew.Header = "Create New Record";
			menuItemCreateNew.Click += (sender, args) => CreateNewRecord();
			menuItemCreateNew.IsEnabled = canEdit;

			var menuItemEdit = new MenuItem();
			_menuItemEdit = menuItemEdit;
			menuItemEdit.Header = "Edit Actual Record";
			menuItemEdit.Click += (sender, args) => EditActualRecord();
			menuItemEdit.IsEnabled = false;

			var contextMenu = new ContextMenu();
			contextMenu.Items.Add(menuItemEdit);
			contextMenu.Items.Add(menuItemCreateNew);
			contextMenu.Items.Add(menuItemSearch);
			ContextMenu = contextMenu;

			_button.Content = "Lookup";
			var groupBox = new GroupBox();
			groupBox.Header = _button;
			groupBox.Content = panel;
			Content = groupBox;

			_button.Focusable = false;
			_button.Click += (a, b) => _ShowContextMenu();
			ContextMenu.Closed += (a, b) =>
			{
				ContextMenu.PlacementTarget = null;
				ContextMenu.VerticalOffset = 0;
			};
		}

		/// <summary>
		/// If key is Down, then open the menu.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
		{
			base.OnPreviewKeyDown(e);

			if (e.Key == System.Windows.Input.Key.Down)
			{
				_ShowContextMenu();
				e.Handled = true;
			}
		}
		private void _ShowContextMenu()
		{
			ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Relative;
			ContextMenu.PlacementTarget = _button;
			ContextMenu.VerticalOffset = _button.ActualHeight;
			ContextMenu.IsOpen = true;
		}

		/// <summary>
		/// Gets or sets a value indicating that this control is read-only.
		/// </summary>
		public bool IsReadOnly
		{
			get
			{
				return _keyControl._recordBoundControl.IsReadOnly;
			}
			set
			{
				bool canEdit = CanEdit();
				_menuItemCreate.IsEnabled = !value && canEdit;
				_menuItemSearch.IsEnabled = !value && CanSearch();
				_keyControl._recordBoundControl.IsReadOnly = value;
				_menuItemEdit.IsEnabled = !value && canEdit && Value != null;
			}
		}

		/// <summary>
		/// Gets or sets the button displayname.
		/// </summary>
		public string DisplayName
		{
			get
			{
				return _button.Content as string;
			}
			set
			{
				_button.Content = value;
			}
		}

		internal abstract bool CanSearch();
		internal abstract bool CanEdit();
		internal abstract void CreateNewRecord();
		internal abstract void EditActualRecord();
		internal abstract void Search();

		internal void OnValueChanged()
		{
			var handler = ValueChanged;
			if (handler != null)
				handler(this, null);
		}

		/// <summary>
		/// Event invoked when the value changes because of a search/new record.
		/// </summary>
		public event RoutedEventHandler ValueChanged;
		
		#region IValueContainer Members
			/// <summary>
			/// Gets or sets the value of this control.
			/// </summary>
			public abstract object Value { get; set; }
		#endregion
		#region IValueControl Members
			/// <summary>
			/// Clears the value of this control.
			/// </summary>
			public abstract void Clear();
		#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