Cia*_*tic 7 .net wpf xaml templatebinding .net-3.5
看起来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>
Run Code Online (Sandbox Code Playgroud)
TemplateBinding Foreground工作得很好,椭圆是绿色的.但StrokeThickness它似乎不起作用,为什么?
小智 14
另一种可能的解决方案......(因为我只想使用IValueConverters作为最后的手段,如果你需要将其设置为其他东西,更改Ellipse的DataContext可能不起作用):
<Ellipse StrokeThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness.Top}" />
Run Code Online (Sandbox Code Playgroud)
这相当于原始intent(绑定到TemplatedParent),但使用long-hand标记允许您指定Path而不仅仅是属性
BorderThickness并不是那么容易,它是一个类型的结构Thickness(并且可以是复合的,像BorderThickness=".0,.0,2,2"),而StrokeThickness属性是类型double.
您需要IValueConverter使此绑定工作.