Reputation: 3243
If I have a ControlTemplate defined:
<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
<Border BorderBrush="Orange" BorderThickness="3" CornerRadius="2"
Background="Red" TextBlock.Foreground="White">
<ContentPresenter RecognizesAccessKey="True" Margin="{TemplateBinding Padding}"/>
</Border>
</ControlTemplate>
and I have it bound to a button control
<WrapPanel>
<Button Margin="10" Padding="5" Template="{StaticResource ButtonTemplate}">Test</Button>
</WrapPanel>
Why is the Padding="5" not honored unless I include the TemplateBinding markup extension? What other properties are ignored and under what circumstances or how do I determine if they will be ignored or honored?
Also, just out of curiosity, not that you WOULD but could you replace a template binding with a normal data binding expression and have it still work? (if so, what would the path be?) I'm still learning WPF and just want to get a deeper understanding of some of the mechanics
Upvotes: 0
Views: 46
Reputation: 12315
Hi this is happening because of feature of Dependency Property. Dependency properties get its values according to the precendence and it is as follows 1) Active or Animation 2)Local (i.e inline) value 3) Templated value 4) Style value 5) Default value . Now above you have Margin Value set for both (local and Templated) but local value has higher precedence and hence this value is applied . This is answer for Why is it happening so I hope this will help.
Upvotes: 0
Reputation: 185117
Why is the Padding="5" not honored unless I include the TemplateBinding markup extension?
That is just how WPF works, the template controls how it uses some of the properties, there is no way for the system to divine where in the template the Padding
needs to come in for it to make sense conceptually so you need to manually bind it. Among others you should bind Margin
and Background
.
A normal binding would look something like:
Margin="{Binding Padding, RelativeSource={RelativeSource TemplatedParent}}"
Upvotes: 1