简体   繁体   中英

How to c# programmatically express XAML TranslateTransform

I don´t understand how to express xaml-code programmatically. How should it been done from this 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>

So I just searched for DoubleAnimation codebehind C# and found what you want to do. Solution

Just for convenience not to click the link, here's the example in the answer :

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);
        }
    }
}

This is how the XAML looks like:

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

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