Bookmark and Share Share...    Subscribe to this feed Feed   About me...


Control Templates

Introduction

Controls in WPF are separated into logic, that defines the states, events and properties and template, that defines the visual appearance of the control. The wireup between the logic and the template is done by DataBinding.

Each control has a default template. This gives the control a basic appearance. The default template is typically shipped together with the control and available for all common windows themes. It is by convention wrapped into a style, that is identified by value of the DefaultStyleKey property that every control has.

The template is defined by a dependency property called Template. By setting this property to another instance of a control template, you can completely replace the appearance (visual tree) of a control.



The control template is often included in a style that contains other property settings. The following code sample shows a simple control template for a button with an ellipse shape.

 
<Style x:Key="DialogButtonStyle" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Ellipse Fill="{TemplateBinding Background}"
                             Stroke="{TemplateBinding BorderBrush}"/>
                        <ContentPresenter HorizontalAlignment="Center"
                                          VerticalAlignment="Center"/>
                </Grid>            
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
 
 
 
<Button Style="{StaticResource DialogButtonStyle}" />
 
 

A Button without and with a custom control template

ContentPresenter

When you create a custom control template and you want to define a placeholder that renders the content, you can use the ContentPresenter. By default it adds the content of the Content property to the visual tree of the template. To display the content of another property you can set the ContentSource to the name of the property you like.

Triggers

{RelativeSource TemplatedParent} not working in DataTriggers of a ControlTemplate

If you want to bind to a property of a property on your control like Data.IsLoaded you cannot use a normal Trigger, since it does not support this notation, you have to use a DataTrigger.

But when you are using a DataTrigger, with {RelativeSource TemplatedParent} it will not work. The reason is, that TemplatedParent can only be used within the ControlTemplate. It is not working in the Trigger section. You have to use the {RelativeSource Self} instead.




 Comments on this article

Show all comments
BALAJI
Commented on 10.March 2009
nice
ike nich
Commented on 8.April 2009
...
Andrei Drynov
Commented on 20.April 2009
I especially liked "Focussed" and "Thikness"

Nice article, Chris!
Christian Moser
Commented on 20.April 2009
Hi Andrei,
I corrected the typos in the graphics ;-)
Thanks for the feedback!
alexlll80
Commented on 7.May 2009
very nice.
Rasbeer
Commented on 13.May 2009
Good one!
khaled
Commented on 17.May 2009
i really like it thankyou
Swathi
Commented on 3.June 2009
Can you give an example for Data Template also?
DaniBoi
Commented on 29.September 2009
How to make something like a ASP .Net Master Page in WPF
Diana
Commented on 23.October 2009
Thanks, your posts are very very useful!
om
Commented on 6.November 2009
Can we partially override the Control Style?
Like in comboBox i dont want ToggleButton but other combobox should be as it is?
jojo
Commented on 7.January 2010
good one!!!
Witschi
Commented on 14.January 2010
Hello your doing a nice job!
I'm beginner and don't understand the trigger part could you add an example please?
Wendell
Commented on 26.January 2010
With what program or software dis you make the graphics? They are aawessooomeee and I'm really jealous ... Oh nice explanation tho thankx man!
estie
Commented on 10.February 2010
So clear,
thankx.
Abirami
Commented on 10.March 2010
Hi Chris,
This is really very useful for me, as I am a beginner..

Thanks for this wonderful site :)

Name
E-Mail (optional)
Comment
About Christian Moser