如何以c#编程方式表示XAML TranslateTransform

如何以c#编程方式表示XAML TranslateTransform,c#,xaml,C#,Xaml,我不明白如何以编程方式表达xaml代码。如何从这个xaml中执行此操作 <TextBlock FontFamily="Vijaya" FontSize="16" Name="textscroll" Margin="35,330,330,21" Foreground="LightSteelBlue" Text="BLA-BLA&#13;bla-bla again"> <TextBlock.RenderTransform>

我不明白如何以编程方式表达xaml代码。如何从这个xaml中执行此操作

    <TextBlock FontFamily="Vijaya" FontSize="16" Name="textscroll" Margin="35,330,330,21" Foreground="LightSteelBlue" Text="BLA-BLA&#13;bla-bla again">
        <TextBlock.RenderTransform>
            <TranslateTransform x:Name="TRANCE_X" />
        </TextBlock.RenderTransform>
        <TextBlock.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                <BeginStoryboard>
                    <Storyboard RepeatBehavior="1x">
                        <DoubleAnimation
                            From="-300" To="0"
                            Storyboard.TargetName="TRANCE_X"
                            Storyboard.TargetProperty="Y"
                            Duration="0:0:1" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </TextBlock.Triggers>
    </TextBlock>

所以我只是在C#后面搜索了DoubleAnimation codebehind,找到了您想要做的事情

为了方便起见,不要点击链接,下面是答案中的示例:

namespace WpfCSharpSandbox
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            WidenObject(150, TimeSpan.FromSeconds(1));
        }

        private void WidenObject(int newWidth, TimeSpan duration)
        {
            DoubleAnimation animation = new DoubleAnimation(newWidth, duration);
            rctMovingObject.BeginAnimation(Rectangle.WidthProperty, animation);
        }
    }
}
以下是XAML的外观:

<Window x:Class="WpfCSharpSandbox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Sandbox" Height="350" Width="525">
    <Grid Background="#333333">
        <Rectangle x:Name="rctMovingObject" Fill="LimeGreen" Width="50" Height="50"/>
    </Grid>
</Window>


用C#实现同样的结果,以编程方式表达它是什么意思?是的。确切地我已经试过了,但是我找不到正确的语法。谢谢你给我一个几何矩形的例子,但是我想在textbox/textblock中做一个基于文本的转换。关于“textblock rendertransform”、“translatetransform”和触发器的使用,我不理解如何将XAML“转换”为c。