0

我有一个用户控件,它创建滚动文本的动画,在我的主窗口上我这样称呼它:

xmlns:mar="clr-namespace:WpfApplication4.AppPages"
<mar:Feed Background="DarkGray" FontSize="12" MarqueeTimeInSeconds="8" 
          Foreground="Gray" Margin="7,383,711,6" MarqueeContent="Live Feed" 
          MarqueeType="TopToBottom"></mar:Feed>

用户控件的代码如下所示:

    MarqueeType _marqueeType;

    public MarqueeType MarqueeType
    {
        get { return _marqueeType; }
        set { _marqueeType = value; }
    }       

    public String MarqueeContent
    {
        set { tbmarquee.Text = value; }
    }

    private double _marqueeTimeInSeconds;

    public double MarqueeTimeInSeconds
    {
        get { return _marqueeTimeInSeconds; }
        set { _marqueeTimeInSeconds = value; }
    }

    public Feed()
    {
        InitializeComponent();
        canMain.Height = this.Height;
        canMain.Width = this.Width;
        this.Loaded += new RoutedEventHandler(Feed_Loaded);
    }

    void Feed_Loaded(object sender, RoutedEventArgs e)
    {
        StartMarqueeing(_marqueeType);
    }

    public void StartMarqueeing(MarqueeType marqueeType)
    {
            TopToBottomMarquee();
    }

    private void TopToBottomMarquee()
    {
        double width = canMain.ActualWidth - tbmarquee.ActualWidth;
        tbmarquee.Margin = new Thickness(width / 2, 0, 0, 0);
        DoubleAnimation doubleAnimation = new DoubleAnimation();
        doubleAnimation.From = -tbmarquee.ActualHeight;
        doubleAnimation.To = canMain.ActualHeight;
        doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
        doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(_marqueeTimeInSeconds));
        tbmarquee.BeginAnimation(Canvas.TopProperty, doubleAnimation);
    }

public enum MarqueeType
{
    TopToBottom
}

在主窗口上,我MarqueeContent="Live Feed"像这样设置 xaml,但是如何在后面的代码中设置内容以及如何设置多个 MarqueeContents?

例如,即使我能够从后面的代码中设置 MarqueeContent 并向其中添加了多个项目,毫无疑问它只会像您刚才阅读的文本一样一个接一个地添加它,我需要它,所以我添加的每个项目都有如果有意义的话,至少有一个段落间距。

要给出一个直观的想法,你可以在这里看到它(TopDown):

http://www.codeproject.com/Articles/48267/Making-a-Simple-Marquee-Text-Control-Drip-Animatio

我需要它,以便可以将多个字符串加载到其中。并且添加的每个文本字符串都由一个段落分隔。

4

1 回答 1

1

如果只是将多行文本添加到一个移动块中,您可以简单地在行之间添加换行符:

textBlock.Text = "A line of text.\n\nAnother line of text.";

或者你可以对Inlines做同样的事情:

textBlock.Inlines.Add(new Run("A line of text."));
textBlock.Inlines.Add(new LineBreak());
textBlock.Inlines.Add(new LineBreak());
textBlock.Inlines.Add(new Run("Another line of text."));
于 2012-04-21T11:32:09.043 回答