我写了这段代码:
<ControlTemplate TargetType="Label">
<Grid Height="30" Width="70" x:Name="grid">
<Border>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Border>
<Separator>
<Separator.Margin>
<Thickness Left="{Binding Path=Width,RelativeSource={RelativeSource AncestorType=Label}}" Top="0" Right="0" Bottom="0"/>
</Separator.Margin>
<Separator.LayoutTransform>
<RotateTransform Angle='120'/>
</Separator.LayoutTransform>
</Separator>
</Grid>
</ControlTemplate>
我想只绑定分隔符的左边距,但是这段代码给了我错误。 还有其他解决办法吗?
答案 0 :(得分:2)
绑定只能应用于DependencyProperty
:
What is a dependency property?
Thickness.Left
不是依赖项属性,因此您必须绑定整个Margin
(其中是依赖项属性)。要仅调整左边缘,您可以创建一个ValueConverter
来获取Width
并返回Thickness
。例如:
<Separator Margin="{Binding RelativeSource={RelativeSource AncestorType=Label},
Path=Width,
Converter={StaticResource MyLeftMarginConverter}}" >
<Separator.LayoutTransform>
...