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?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Ciantic
  • 6,064
  • 4
  • 54
  • 49

4 Answers4

16

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

karthikr
  • 97,368
  • 26
  • 197
  • 188
ncsu95
  • 778
  • 7
  • 6
  • 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](https://stackoverflow.com/a/8522026/1518546) but [the order is the same for Thickness](https://learn.microsoft.com/en-us/dotnet/api/system.windows.thickness#xaml-values). – John Cummings Feb 13 '18 at 23:41
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.

Massimiliano
  • 16,770
  • 10
  • 69
  • 112
1

You can also use the DataContext property of the Ellipse:

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

Hope this helps!

Horacio Nuñez
  • 256
  • 1
  • 7
1

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

Ciantic
  • 6,064
  • 4
  • 54
  • 49