#10 – Control Templates

Similar to styles in WPF, but different, are templates.  Templates allow you to replace all aspects of a UI control’s appearance without changing its behavior.

Every control in WPF has a default template that completely specifies its appearance.  You can replace the default template with a template that you’ve authored, to change the appearance of the control.

You change a control’s template by changing its Template property.  Here’s an example where we create a new button template that gives the button a thick blue border.

<Window.Resources>
    <ControlTemplate x:Key="Crazy" TargetType="Button">
        <Border BorderBrush="Blue" Background="White" BorderThickness="3">
            <ContentPresenter></ContentPresenter>
        </Border>
    </ControlTemplate>
</Window.Resources>
<Grid Height="auto" Width="503">
    <StackPanel Height="100" HorizontalAlignment="Left" Margin="85,139,0,0" Name="stackPanel1"  >
        <Button Content="Crazy template" Template="{StaticResource Crazy}"  />
        <Button Content="Default template" />
    </StackPanel>
</Grid>

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

One Response to #10 – Control Templates

  1. Pingback: #267 – Think Twice Before You Subclass a Control « 2,000 Things You Should Know About WPF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: