How to TemplateBind to BorderThickness.Top (or Bottom or Left or Right)?
Asked Answered
D

2

10

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.

Doubloon answered 8/9, 2009 at 8:43 Comment(0)
R
16

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.

Rakehell answered 8/9, 2009 at 9:14 Comment(1)
ahaa... thank you very much 4 your quick and accurate reply... i recently learnt about dependency properties... how can i forget that?!Doubloon
M
1

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>
Montez answered 25/3, 2017 at 14:1 Comment(1)
This does not work, because Thickness.Top is not a DependencyProperty. It results in a XAMLParseException.Hsining

© 2022 - 2024 — McMap. All rights reserved.