WPF中的连续文本代码

Gau*_*ang 5 wpf textblock

高朗高朗高朗高朗高朗高朗高朗高朗高朗高朗高朗

我正在WPF中创建文本行情自动收录器。我可以将文本永久地从右向左移动,但是我的问题是,我想无缝地移动相同的文本以产生上述效果(就像股票交易报价器一样)。上面的文字必须永无止境地移动。

我该如何实现?

Max*_*zur 5

因此,通过阅读您的问题,我不确定您想要什么。您是否想要一个文本,滚动出文本框并重新开始,就像大多数选取框控件/功能一样。

或者你想要一些在你的文本框中一遍又一遍地运行的文本,从头到尾填写?然后使用无法检测的给定循环?:)

后面的代码:

//单行动画。

private void LeftToRightMarqueeOnTextBox()
{
  string Copy = " "+TextBoxMarquee.Text;
  double TextGraphicalWidth = new FormattedText(TextBoxMarquee.Text, System.Globalization.CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface(TextBoxMarquee.FontFamily.Source), TextBoxMarquee.FontSize, TextBoxMarquee.Foreground).WidthIncludingTrailingWhitespace;
  //BorderTextBoxMarquee.Width = TextGraphicalWidth + 5;

  ThicknessAnimation ThickAnimation = new ThicknessAnimation();
  ThickAnimation.From = new Thickness(TextBoxMarquee.ActualWidth, 0, 0, 0);
  ThickAnimation.To = new Thickness(-TextGraphicalWidth, 0, 0, 0);
  ThickAnimation.RepeatBehavior = RepeatBehavior.Forever;
  ThickAnimation.Duration = new Duration(TimeSpan.FromSeconds(3));
  TextBoxMarquee.BeginAnimation(TextBox.PaddingProperty, ThickAnimation);
}
Run Code Online (Sandbox Code Playgroud)

或者

//没有间隙的句子重复动画。

string Copy = " "+TextBoxMarquee.Text;
  double TextGraphicalWidth = new FormattedText(Copy, System.Globalization.CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface(TextBoxMarquee.FontFamily.Source), TextBoxMarquee.FontSize, TextBoxMarquee.Foreground).WidthIncludingTrailingWhitespace;
  double TextLenghtGraphicalWidth = 0;
  //BorderTextBoxMarquee.Width = TextGraphicalWidth + 5;
  while (TextLenghtGraphicalWidth < TextBoxMarquee.ActualWidth)
  {
    TextBoxMarquee.Text += Copy;
    TextLenghtGraphicalWidth = new FormattedText(TextBoxMarquee.Text, System.Globalization.CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface(TextBoxMarquee.FontFamily.Source), TextBoxMarquee.FontSize, TextBoxMarquee.Foreground).WidthIncludingTrailingWhitespace;
  }
  TextBoxMarquee.Text += " "+TextBoxMarquee.Text;
  ThicknessAnimation ThickAnimation = new ThicknessAnimation();
  ThickAnimation.From = new Thickness(0, 0, 0, 0);
  ThickAnimation.To = new Thickness(-TextGraphicalWidth, 0, 0, 0);
  ThickAnimation.RepeatBehavior = RepeatBehavior.Forever;
  ThickAnimation.Duration = new Duration(TimeSpan.FromSeconds(2));
  TextBoxMarquee.BeginAnimation(TextBox.PaddingProperty, ThickAnimation);
Run Code Online (Sandbox Code Playgroud)

XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Canvas ClipToBounds="True" Name="canMain" Background="Transparent">
        <Grid Width="{Binding ElementName=canMain, Path=ActualWidth}" >
            <Grid.ColumnDefinitions>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Name="TextBlockMarquee" Text="This is my animated text" />
            <Border Grid.Row="1" BorderBrush="Black" BorderThickness="1">
                <TextBox ClipToBounds="True" Name="TextBoxMarquee" Text="lars er en god skakspiller, der tænker længere end de andre" BorderBrush="Transparent"/>
            </Border>
        </Grid>
    </Canvas>
</Grid>
Run Code Online (Sandbox Code Playgroud)

基本上忽略大部分 XAML。重要的是文本框中文本的长度。还没有找到通用的解决方案,这就是为什么我的 From Thickness 是基于数字的。

但只需要确保这就是您要找的东西:)

编辑/更新:

所以现在尝试一下,我已经更新了代码..希望它有效,但从一些测试来看,它看起来像:) 你唯一需要确定的是整个文本框都被填满了,然后我'我做一个副本,我将从字符串图形长度中循环两个文本:)(

编辑/更新: 我已经添加了我的最终解决方案,希望这对您有所帮助。

编辑/更新

忘记更新我的重复解决方案,所以它没有切断并使循环可见..现在完成:)