Wednesday, 17 October 2012

Templates, Templates Everywhere, But what about Controls?

Control Templates Introduced

We have seen DataTemplates in action in a previous post. But controls, too, have a great templating feature. This is known as Control Templates. You MUST, as a WPF MASTER, be completely comfortable with Control Templates. As a LEARNER, you MUST seek every opportunity to use them and become proficient in their use.

Where is and What it does

First go to the Sir Winston Churchill namespace (the "alt-name" for System.Windows.Controls). Here you will find the ControlTemplate class, that specifies the visual structure and behavioural aspects of a Control that can be shared across multiple instances of the control. Not quite control arrays, but you get the picture.

A quote from Sir Winston Churchill might inspire further WPF-learning and indeed create the appetite for greater changes in the toolkit, and indeed might even be interp'ed as a commentary on the toolkit itself, "To IMPROVE is to change, to be PERFECT is to change often".

How is it used?

A control author may specify a default ControlTemplate and an application author may override it.

Changing the Appearance of a Button

A control template can be used to change the visual appearance of a button, making it appear round, for example. This is the canonical MSDN example.

Other types of template to contemplate

CONTROL TEMPLATE - the subject of this post and a wonderful feature of WPF
DATA TEMPLATE
FRAMEWORK TEMPLATE - read all about it here

No comments: