O'Reilly logo

WPF Programmer's Reference: Windows Presentation Foundation with C# 2010 and .NET 4 by Rod Stephens

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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"/> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required