Download Silverlight Process Designer in SharePoint WSS
Transcript
8.6 Using Control Templates
Templates are for Control-based classes and are used for changing how the controls are
rendered visually. This is possible because there is a separation in how they look and what
they do.
Each control can have different states, such as disabled. A control template lets the developer
define what the control looks like in each of its states. It is a kind of changing the look and
feel of the control because the visual appearance changes when the user interacts with it.
Every Control in Silverlight has a property Template which is located in the Control class. By
setting this property the developer is resetting the controls appearance. From this reset state
the look and feel can be adapted as wished. The new appearance that is defined in the below
code sample is a round close button.
<Style x:Key="CloseButton" TargetType="Button">
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="Width" Value="15"/>
<Setter Property="Height" Value="15"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="brd1" Width="14" Height="14" CornerRadius="15">
<Border.Background>
<RadialGradientBrush GradientOrigin=".3, .3">
<GradientStop Color="#FFF" Offset=".15"/>
<GradientStop Color="#777" Offset="1"/>
</RadialGradientBrush>
</Border.Background>
<TextBlock x:Name="txt1" Foreground="#222" TextAlignment="center"
Text="r" FontSize="9" VerticalAlignment="center"
FontFamily="Webdings"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The button above is forced to display the letter r as a Webdings font, which is a
representing close. If the button was to be used as a general purpose button control
template it wouldn’t be a very useful, because a new control template would have to be
defined for each text that the button presents. This issue is solved by using the
TemplateBinding markup extension. When using the TemplateBinding markup extension for
a button a special class called the ContentPresenter has to be used. This class makes it
possible to display all kind of content that is possible for a button but also other controls
Content property. By changing the TextBlock tag above by the one below this is
accomplished.
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"
Content="{TemplateBinding Content}" />
The button control template above does not give the user any visual feedback reflecting the
different visual states a Button control can have. By using something called the Visual State
Manager defining the different states is possible. Each control has a set of visual state groups
and visual states defined to them. The states within the group are mutually exclusive but the
35