【问题标题】:How to add scrolling/moving text in textblock in xaml如何在 xaml 的文本块中添加滚动/移动文本
【发布时间】:2015-05-28 17:34:02
【问题描述】:

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

我在网上找到了这段代码,但这不是我想要的。我想滚动文本 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>

【问题讨论】:

  • 这是一个问题,还是只是要求代码?
  • 索取代码..

标签: c# xaml windows-store-apps


【解决方案1】:

你可以试试这样的..

这是一个 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();

    }

【讨论】:

  • 我希望文本从右向左移动..有什么想法吗??
  • FlowDirection="RightToLeft"
  • 它给出错误“在类型'ScrollViewer'中找不到属性'CanContentScroll'。”现在该怎么办??..请告诉
猜你喜欢
  • 1970-01-01
  • 2016-11-08
  • 2021-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-10
  • 2011-09-14
相关资源
最近更新 更多