Xaml:仅绑定左边距

时间:2014-03-08 14:16:02

标签: c# .net wpf xaml margin

我写了这段代码:

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

我想只绑定分隔符的左边距,但是这段代码给了我错误。 还有其他解决办法吗?

1 个答案:

答案 0 :(得分:2)

绑定只能应用于DependencyPropertyWhat is a dependency property?

Thickness.Left不是依赖项属性,因此您必须绑定整个Margin(其中依赖项属性)。要仅调整左边缘,您可以创建一个ValueConverter来获取Width并返回Thickness。例如:

<Separator Margin="{Binding RelativeSource={RelativeSource AncestorType=Label},
                            Path=Width,
                            Converter={StaticResource MyLeftMarginConverter}}" >
    <Separator.LayoutTransform>
        ...