如何在xaml中的textblock中添加滚动/移动文本

时间:2015-05-28 17:34:03

标签: c# xaml windows-store-apps

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

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

<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 个答案:

答案 0 :(得分:4)

你可以尝试这样的事情......

这是一个xaml示例:

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

    }