How to add scrolling/moving text in textblock in xaml

南笙酒味 提交于 2019-12-22 18:34:11

问题


I want to add a scrolling/moving text(from right to left) in the textblock. It should scroll one time only. I have Googled it but didn't find anything. I want to scroll the text in the textblock (not the whole textblock) only once. Then scrolling should be stopped.

I found this code on net but it is not what I want. I want to scroll the text 1 time and then stop scrolling. Any idea how to do that?

<TextBlock FontSize="22" x:Name="txtScrolling" Margin="1386,208,-616,460">
            <TextBlock.RenderTransform>
                <TranslateTransform x:Name="translate" />
            </TextBlock.RenderTransform>
            <TextBlock.Triggers>
                <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                    <BeginStoryboard>
                        <Storyboard RepeatBehavior="1">
                            <DoubleAnimation
                        From="1000" To="-1000"
                        Storyboard.TargetName="translate"
                        Storyboard.TargetProperty="X"
                        Duration="0:0:10" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBlock.Triggers>
    This is the Text to Scroll
        </TextBlock>

回答1:


You could try something like this..

This is an xaml example:

<Grid>
    <ScrollViewer x:Name="Scroll_Content" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto" CanContentScroll="True" VerticalAlignment="Center" HorizontalAlignment="Center" Width="250" FlowDirection="RightToLeft">
        <TextBlock Text="Hello World ------------ Hello World ------------ Hello World -------- Hello World"></TextBlock>
    </ScrollViewer>
    <Button x:Name="Start_Timer" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,10" Width="100" Height="50" Content="Start Timer" Click="Start_Timer_Click"/>
</Grid>

Code behind:

    DispatcherTimer timer1 = new DispatcherTimer();
    double num = 0;

    public MainWindow()
    {
        InitializeComponent();

        timer1.Interval = new TimeSpan(0,0,0,0,250);
        timer1.Tick += timer1_Tick;
    }

    void timer1_Tick(object sender, EventArgs e)
    {
        try
        {
            Scroll_Content.ScrollToHorizontalOffset(num);
            num++;
        }
        catch { }
    }

    void Start_Timer_Click(object sender, RoutedEventArgs e)
    {
        if (timer1.IsEnabled == false) 
        {
            num = 0;
            timer1.Start();
        }
        else
            timer1.Stop();

    }


来源:https://stackoverflow.com/questions/30513419/how-to-add-scrolling-moving-text-in-textblock-in-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!