WPF:TemplateBinding 到 Shape 的 StrokeThickness 不起作用?

发布于 2024-08-12 18:00:29 字数 978 浏览 10 评论 0原文

看起来 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>

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清晰传感 2024-08-19 18:00:29

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

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

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

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

何以笙箫默 2024-08-19 18:00:29

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

您需要 IValueConverter 才能使此绑定起作用。

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.

青春有你 2024-08-19 18:00:29

有一个命名陷阱:BorderThicknessThickness 的类型,而 StrokeThicknessdouble 的类型。所以我们需要IValueConverter

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

梦幻的心爱 2024-08-19 18:00:29

您还可以使用椭圆的 DataContext 属性:

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

希望这有帮助!

You can also use the DataContext property of the Ellipse:

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

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文