如何在 xaml 的文本块中添加滚动/移动文本 [英] How to add scrolling/moving text in textblock in xaml

查看:24
本文介绍了如何在 xaml 的文本块中添加滚动/移动文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本块中添加滚动/移动文本(从右到左).它应该只滚动一次.我已经谷歌搜索但没有找到任何东西.我只想滚动文本块(而不是整个文本块)中的文本一次.然后应该停止滚动.

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.

我在网上找到了这段代码,但这不是我想要的.我想滚动文本 1 次,然后停止滚动.知道怎么做吗?

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>

推荐答案

你可以试试这样的..

这是一个 xaml 示例:

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>

背后的代码:

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

    }

这篇关于如何在 xaml 的文本块中添加滚动/移动文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆