简体   繁体   中英

Xaml:Binding only left margin

I have written this code:

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

I want to bind only left margin of separator, but this code gives me error. Any other solution?

A Binding can only be applied to a DependencyProperty : What is a dependency property?

Thickness.Left isn't a dependency property, so you'll have to bind the whole Margin (which is a dependency property). To adjust only the left edge, you could create a ValueConverter that takes the Width and returns a Thickness . Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM