1
Answer

ControlTemplate TemplateBinding Newbie Question

Ask a question
martin holmes

martin holmes

14y
4.3k
1
Hi 
I am bit confused about TemplateBinding in a ControlTemplate and was wondering if someone can help me get things straight.

It's my understanding that a TemplateBinding allows you to reach back into the control to obtain the value for a property.

For example in the below xaml the BorderThickness="{TemplateBinding BorderThickness} reaches into the Button control for the BorderThickness of 2.

When I remove the BorderThickness TemplateBinding the border on the button disappears.

What confuses me is that there are no such Template Bindings for Height and Width yet when I change these values on the Button control 
the Buttons size changes! 

Why does this happen even though I havn't bound Width and Height?
i.e. Width="{TemplateBinding Width} and Height="{TemplateBinding Height}
thanks in advance!


<Window x:Class="TemplatesTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ControlTemplate x:Key="MyButtonTemplate" TargetType="{x:Type Button}">
<Grid>
<Border x:Name="thisborder" CornerRadius="10" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" />
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="{TemplateBinding Padding}" />
</Grid>

</ControlTemplate>
</Window.Resources>
<Grid>
<Button BorderThickness="2" Template="{StaticResource MyButtonTemplate}" Width="150px" Height="40px" Content="Click Me!" >

</Button>
</Grid>
</Window>


Answers (1)