9

看起来 ControlTemplate 中的以下 Ellipse 没有得到 BorderThickness,但为什么呢?

<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>

TemplateBindingForeground工作得很好,椭圆是绿色的。但StrokeThickness它似乎不起作用,为什么?

4

4 回答 4

16

另一种可能的解决方案......(因为我喜欢只使用 IValueConverters 作为最后的手段,如果你需要将它设置为其他东西,更改 Ellipse 的 DataContext 可能不起作用):

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

这等同于最初的意图(绑定到 TemplatedParent),但使用长手标记允许您指定 Path 而不仅仅是属性

于 2012-10-09T19:12:46.033 回答
7

BorderThickness不是那么容易,它是一个类型的结构Thickness(并且可以是复合的,比如BorderThickness=".0,.0,2,2"),而StrokeThicknessproperty 是类型的double

你需要IValueConverter使这个绑定工作。

于 2009-11-26T14:35:27.407 回答
1

有命名问题:BorderThicknessis type of ThicknessStrokeThicknessis type of double。所以我们需要IValueConverter.

于 2009-11-26T14:41:15.507 回答
1

您还可以使用 Ellipse 的 DataContext 属性:

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

希望这可以帮助!

于 2012-06-14T19:22:10.423 回答