Click here to Skip to main content
15,886,840 members
Articles / Desktop Programming / WPF

Creating a look-less custom control in WPF

Rate me:
Please Sign up or sign in to vote.
4.86/5 (60 votes)
4 Jun 2006CPOL3 min read 193.1K   3.7K   137  
In WPF, custom controls should be look-less. That means, that business logic and UI are strongly separated. When you create a control, you can provide a default template for the control, but anyone who uses the control can override it without touching the business logic.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Timers;
using System.Windows.Threading;

namespace CustomControlLibrary
{
	/// <summary>
	/// ========================================
	/// WinFX Custom Control
	/// ========================================
	///
	/// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
	///
	/// Step 1a) Using this custom control in a XAML file that exists in the current project.
	/// Add this XmlNamespace attribute to the root element of the markup file where it is 
	/// to be used:
	///
	///     xmlns:MyNamespace="clr-namespace:CustomControlLibrary"
	///
	///
	/// Step 1b) Using this custom control in a XAML file that exists in a different project.
	/// Add this XmlNamespace attribute to the root element of the markup file where it is 
	/// to be used:
	///
	///     xmlns:MyNamespace="clr-namespace:CustomControlLibrary;assembly=CustomControlLibrary"
	///
	/// You will also need to add a project reference from the project where the XAML file lives
	/// to this project and Rebuild to avoid compilation errors:
	///
	///     Right click on the target project in the Solution Explorer and
	///     "Add Reference"->"Projects"->[Browse to and select this project]
	///
	///
	/// Step 2)
	/// Go ahead and use your control in the XAML file. Note that Intellisense in the
	/// XML editor does not currently work on custom controls and its child elements.
	///
	///     <MyNamespace:Clock/>
	///
	/// </summary>

	public class Clock : Control
	{
		private DispatcherTimer timer;

		static Clock()
		{
			//This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
			//This style is defined in themes\generic.xaml
			DefaultStyleKeyProperty.OverrideMetadata(typeof(Clock), new FrameworkPropertyMetadata(typeof(Clock)));
		}

		protected override void OnInitialized(EventArgs e)
		{
			base.OnInitialized(e);

			UpdateDateTime();

			timer = new DispatcherTimer();
			timer.Interval = TimeSpan.FromMilliseconds(1000 - DateTime.Now.Millisecond);
			timer.Tick += new EventHandler(Timer_Tick);
			timer.Start();
		}

		private void Timer_Tick(object sender, EventArgs e)
		{
			UpdateDateTime();

			timer.Interval = TimeSpan.FromMilliseconds(1000 - DateTime.Now.Millisecond);
			timer.Start();
		}

		private void UpdateDateTime()
		{
			this.DateTime = System.DateTime.Now;
		}

		#region DateTime property
		public DateTime DateTime
		{
			get
			{
				return (DateTime)GetValue(DateTimeProperty);
			}
			private set
			{
				SetValue(DateTimeProperty, value);
			}
		}

		public static DependencyProperty DateTimeProperty = DependencyProperty.Register(
				"DateTime",
				typeof(DateTime),
				typeof(Clock),
				new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(OnDateTimeInvalidated)));

		public static readonly RoutedEvent DateTimeChangedEvent =
			EventManager.RegisterRoutedEvent("DateTimeChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<DateTime>), typeof(Clock));

		protected virtual void OnDateTimeChanged(DateTime oldValue, DateTime newValue)
		{
			RoutedPropertyChangedEventArgs<DateTime> args = new RoutedPropertyChangedEventArgs<DateTime>(oldValue, newValue);
			args.RoutedEvent = Clock.DateTimeChangedEvent;
			RaiseEvent(args);
		}

		private static void OnDateTimeInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			Clock clock = (Clock)d;

			DateTime oldValue = (DateTime)e.OldValue;
			DateTime newValue = (DateTime)e.NewValue;

			clock.OnDateTimeChanged(oldValue, newValue);
		}
		#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 software architects
Austria Austria
Hi, my name is Karin Huber. Since 1998 I have been working as a developer and IT consultant focusing on building database oriented web applications. In 2007 my friend Rainer and I decided that we want to build a business based on COTS (component off-the-shelf) software. As a result we founded "software architects".

These days we are offering our first version of the time tracking software called 'time cockpit'. You can find more information at www.timecockpit.com.

Comments and Discussions