将多个文本字符串添加到由段落分隔的选取框中

时间:2012-04-21 06:57:17

标签: c# wpf xaml user-controls marquee

我有一个用户控件,可以创建一个滚动文本的动画,在我的主窗口上我称之为:

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
}

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

例如即使我能够从代码后面设置MarqueeContent并且我添加了多个项目,毫无疑问只会像你刚才读到的那样一个接一个地添加它,我需要它以便每个项目如果有意义,我添加至少一个段落间距。

要给出一个视觉概念,你可以在这里看到它(TopDown):

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

我需要它,所以我可以加载多个字符串。并且添加的每个文本字符串都以段落分隔。

1 个答案:

答案 0 :(得分: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."));