在WPF中的Style内部RenderTransform绑定

时间:2015-06-08 07:28:00

标签: c# wpf binding wpf-style

我有以下代码。为什么Text - 绑定工作而TranslateTransform.X没有?

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="WpfApplication2.MainWindow"
        Title="MainWindow">

    <Window.Resources>
        <Style TargetType="TextBlock" x:Key="txtStyle">
            <Setter Property="HorizontalAlignment" Value="Left"/>

            <Setter Property="Text" Value="{Binding RelativeSource={RelativeSource Self}, Path=Width, Mode=OneWay}"/>

            <Setter Property="RenderTransform">
                <Setter.Value>
                    <TranslateTransform X="{Binding RelativeSource={RelativeSource Self}, Path=Width, Mode=OneWay}"/>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>
        <TextBlock Canvas.Top="50" Background="Lime" Width="200" Style="{StaticResource txtStyle}"/>
        <TextBlock Canvas.Top="100" Background="LightBlue" Width="300" Style="{StaticResource txtStyle}"/>
    </StackPanel>
</Window>

1 个答案:

答案 0 :(得分:2)

您需要<TranslateTransform X="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBlock}, Path=Width, Mode=OneWay}"/> 绑定。

git remote add upstream https://github.com/adambure21/project_name.git