I wonder if it is possible to bind a structure element like BorderThickness.Top to TemplatedParent's corresponding property. I have tried
<Border Margin="0" Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}">
<Border.BorderThickness>
<Thickness Left="0" Right="0" Top="{TemplateBinding BorderThickness.Top}" Bottom="{TemplateBinding BorderThickness.Bottom}"/>
</Border.BorderThickness>
</Border>
The reason i want to do this is i want Left and Right to be 0 and only Top and Bottom be bound.
Thanks in advance.
This is not possible because Thickness is a value-type - you can only create bindings on dependency properties of dependency objects.
What you could do is binding BorderThickness as normal:
<Border Margin="0"
Padding="{TemplateBinding Padding}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessConverter}}" />
then use a converter to return an appropriately modified Thickness:
object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
{
var thickness = (Thickness) value;
return new Thickness( 0.0, thickness.Top, 0.0, thickness.Bottom );
}
You could even use ConverterParameter to specify which parts of the Thickness to clear.
The solution with a converter is the right one.
In the case where you are interested only in one value, you can do this directly in the XAML without a converter. {TemplateBinding …}
is only a syntactic sugar for {Binding RelativeSource={RelativeSource TemplatedParent} …}
with a limited functionality.
For example some custom border:
<Button BorderBrush="Purple"
BorderThickness="1 2 3 4"
Content="This is a button!">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<DockPanel>
<Rectangle DockPanel.Dock="Left"
Width="{Binding BorderThickness.Left, RelativeSource={RelativeSource TemplatedParent}}"
Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
<Rectangle DockPanel.Dock="Top"
Height="{Binding BorderThickness.Top, RelativeSource={RelativeSource TemplatedParent}}"
Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
<Rectangle DockPanel.Dock="Right"
Width="{Binding BorderThickness.Right, RelativeSource={RelativeSource TemplatedParent}}"
Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
<Rectangle DockPanel.Dock="Bottom"
Height="{Binding BorderThickness.Bottom, RelativeSource={RelativeSource TemplatedParent}}"
Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
<ContentPresenter />
</DockPanel>
</ControlTemplate>
</Button.Template>
</Button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With