从左到右为文本框设置动画,然后重复

时间:2019-01-06 11:36:15

标签: wpf xaml animation

我开始在wpf中玩动画,我正在尝试以下方法:

My Title 
 My Title 
e  My Titl 
le My Tit
tle My Ti

我的意思是标题将从左向右移动。

enter image description here

现在我有:

 <TextBlock  Name="MyWipedText"                                             
                Width="80"
                FontSize="35"
                Foreground="White"                                      
                Text="My Title"  >                

        <TextBlock.Triggers>
            <EventTrigger RoutedEvent="TextBlock.Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation
        Storyboard.TargetName="MyWipedText"                           
        Storyboard.TargetProperty="(TextBlock.Width)"
       To="0.0" Duration="0:0:4"                               
        AutoReverse="False" RepeatBehavior="Forever" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </TextBlock.Triggers>
    </TextBlock>

Related link

1 个答案:

答案 0 :(得分:0)

使用类似TextBlock的

<TextBlock x:Name="textBlock" Text="Hello, World. "/>

您可以使用DispatcherTimer轻松为文本设置动画:

public MainWindow()
{
    InitializeComponent();

    var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0.1) };
    timer.Tick += (s, e) =>
    {
        var text = textBlock.Text;
        var last = text.Length - 1;
        textBlock.Text = text.Substring(last, 1) + text.Substring(0, last);
    };
    timer.Start();
}