WPF: TemplateBinding to StrokeThickness of Shape does not work?
Asked Answered
L

4

9

Looks like the following Ellipse in ControlTemplate does not get the BorderThickness, but why?

<Window.Resources>
    <ControlTemplate x:Key="EllipseControlTemplate" TargetType="{x:Type TextBox}">
        <Grid>
            <Ellipse 
                Width="{TemplateBinding ActualWidth}" 
                Height="{TemplateBinding ActualHeight}" 
                Stroke="{TemplateBinding Foreground}" 
                StrokeThickness="{TemplateBinding BorderThickness}" />
                <ScrollViewer Margin="0" x:Name="PART_ContentHost" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <TextBox
        Template="{DynamicResource EllipseControlTemplate}" 
        Foreground="Green"
        BorderThickness="15" />
</Grid>

TemplateBinding to Foreground works just fine, the ellipse is green. But to StrokeThickness it doesn't seem to work, why?

Ladonna answered 26/11, 2009 at 14:26 Comment(0)
H
8

BorderThickness is not that easy, it is a struct of type Thickness (and can be composite, like BorderThickness=".0,.0,2,2"), while StrokeThickness property is of type double.

You need IValueConverter to make this binding work.

Hamlin answered 26/11, 2009 at 14:35 Comment(3)
I apologize, I found the answer myself already. See the answer I wrote... You probably meant that.Ladonna
Good for you :) Still, mine was earlier :)Hamlin
Thats true! I will choose yours as solution if you mention the Type difference, it's the key here.Ladonna
A
17

Another possible solution ... (because i like to only use IValueConverters as a last resort, and changing the DataContext of the Ellipse might not work if you need it to be set to something else):

<Ellipse StrokeThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness.Top}" />

This is equivalent to the original intent (to bind to the TemplatedParent), but using the long-hand markup allows you to specify a Path rather than just a property

Alkyd answered 9/10, 2012 at 19:12 Comment(1)
Nit-pick on this great answer: I think BorderThickness.Left actually be the preferred value to use since it is the first value. See this answer about margin but the order is the same for Thickness.Jerz
H
8

BorderThickness is not that easy, it is a struct of type Thickness (and can be composite, like BorderThickness=".0,.0,2,2"), while StrokeThickness property is of type double.

You need IValueConverter to make this binding work.

Hamlin answered 26/11, 2009 at 14:35 Comment(3)
I apologize, I found the answer myself already. See the answer I wrote... You probably meant that.Ladonna
Good for you :) Still, mine was earlier :)Hamlin
Thats true! I will choose yours as solution if you mention the Type difference, it's the key here.Ladonna
L
1

There was naming gotcha: BorderThickness is type of Thickness, and StrokeThickness is type of double. So we need IValueConverter.

Ladonna answered 26/11, 2009 at 14:41 Comment(0)
P
1

You can also use the DataContext property of the Ellipse:

<Ellipse DataContext="{TemplateBinding BorderThickness}" StrokeThickness="{Binding Top}" />

Hope this helps!

Pham answered 14/6, 2012 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.