WPF:TemplateBinding 到 Shape 的 StrokeThickness 不起作用?
看起来 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种可能的解决方案...(因为我喜欢只使用 IValueConverters 作为最后的手段,并且如果您需要将其设置为其他内容,则更改 Ellipse 的 DataContext 可能不起作用):
这相当于最初的意图(到绑定到 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):
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
BorderThickness
并不那么容易,它是一个Thickness
类型的结构体(并且可以是复合的,如BorderThickness=".0,.0,2,2"
),而StrokeThickness
属性的类型为double
。您需要
IValueConverter
才能使此绑定起作用。BorderThickness
is not that easy, it is a struct of typeThickness
(and can be composite, likeBorderThickness=".0,.0,2,2"
), whileStrokeThickness
property is of typedouble
.You need
IValueConverter
to make this binding work.有一个命名陷阱:
BorderThickness
是Thickness
的类型,而StrokeThickness
是double
的类型。所以我们需要IValueConverter
。There was naming gotcha:
BorderThickness
is type ofThickness
, andStrokeThickness
is type ofdouble
. So we needIValueConverter
.您还可以使用椭圆的 DataContext 属性:
希望这有帮助!
You can also use the DataContext property of the Ellipse:
Hope this helps!