Appendix N. Templates

This appendix summarizes control templates. For more detailed information, see Chapter 15.

A control template defines a control's constituent controls and how they behave. Typically a ControlTemplate object contains the controls that make up the template. The ControlTemplate can have a Triggers section that determines the control's behavior, and it can have a Resources section to define necessary resources.

The following sections show examples of simple templates for different kinds of controls. You can use these as starting points for your own templates.

Label

The following template makes a Label control display its contents in a wrapped TextBlock. It provides one Trigger that grays out the contents and covers the control with translucent gray stripes when IsEnabled is False.

Label
<ControlTemplate x:Key="temWrappedLabel" TargetType="Label"> <Grid> <Border Name="brdMain" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <TextBlock Name="txtbContent" Margin="4" TextWrapping="Wrap" Text="{TemplateBinding ContentPresenter.Content}"/> </Border> <Canvas Name="canDisabled" Opacity="0"> <Canvas.Background> <LinearGradientBrush StartPoint="0,0" EndPoint="3,3" MappingMode="Absolute" SpreadMethod="Repeat"> <GradientStop Color="LightGray" Offset="0"/> <GradientStop Color="Black" Offset="1"/> ...

Get WPF Programmer's Reference: Windows Presentation Foundation with C# 2010 and .NET 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.