c# - WPF 中 Style 内的 RenderTransform 绑定(bind)

标签 c# wpf binding wpf-style

我有以下代码。为什么 Text-Binding 有效,而 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>

最佳答案

您需要一个 FindAncestor 绑定(bind)。

<TranslateTransform X="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBlock}, Path=Width, Mode=OneWay}"/>

关于c# - WPF 中 Style 内的 RenderTransform 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30703366/

相关文章:

c# - 映射 Fluent NHibernate,其中主键也应该是外键

c# - 不接受 TableAdapter 中的参数

c# - OrderedDictionary 和正则表达式

c# - 在哪里提出 NotifyPropertyChanged?

binding - wpf 窗口宽度不绑定(bind)

c# - 如何让设置函数在 NUnit 中的每个测试 session 只运行一次?

wpf - PowerShell 和 WPF GUI 卡住

c# - MVVM:从头到尾的教程?

ruby-on-rails - Rails : run code ignoring binding. pry

WPF/XAML - 如果另一个控件的文本属性为空/未设置,则隐藏图像