系统地删除第一行

时间:2015-04-05 20:22:39

标签: c# flowdocument paragraph

当整行超过预定数量的条目时,我试图删除段落的第一行。这是一种聊天窗口,我不希望一次显示太多行。

private Paragraph paragraph = new Paragraph();
public void WriteMessage(string output)
    {
string outputFormat = string.Format("{0}", output);
            string[] parts = output.Split(new char[]{':'}, 2);
            string user = parts[0];
            string[] username = parts[0].Split('!');
            paragraph.Inlines.Add(new Run(username[0].Trim() + ": "){Foreground = UserColor});
            paragraph.Inlines.Add(new Run(parts[1]) { Foreground = MessageColor});
            paragraph.Inlines.Add(new LineBreak());

if (paragraph.Inlines.Count >= 50) { 
                //???
                //The count does not actually count lines the way I would expect.
            }
}

不确定最简单的方法,到目前为止我尝试的一切都没有用。

2 个答案:

答案 0 :(得分:0)

建议您使用List vs array。它为您提供了所需的一些功能。

    public List<string> TrimParagraph(List<string> paragraph)
    { 
        int count = paragraph.Count;

        if (count > 50)
            paragraph = paragraph.Skip(count - 50).ToList();

        return paragraph;
    }

编辑...在构造段落对象时使用类似的东西。

答案 1 :(得分:0)

通过创建FlowDocument并将段落添加到块来解决它。然后每个条目都是它自己的块,它保留原始格式。

private Paragraph paragraph = new Paragraph();
_rtbDocument = new FlowDocument(paragraph);

public void WriteMessage(string output)
    {
        string outputFormat = string.Format("{0}", output);
        string[] parts = output.Split(new char[]{':'}, 2);
        string user = parts[0];
        string[] username = parts[0].Split('!');

        Paragraph newline = new Paragraph();

        newline.LineHeight = 2;
        newline.Inlines.Add(new Run(username[0].Trim() + ": ") { Foreground = UserColor });
        newline.Inlines.Add(new Run(parts[1]) { Foreground = MessageColor });

        _rtbDocument.Blocks.Add(newline);

        if (_rtbDocument.Blocks.Count > 10) 
            { 
               _rtbDocument.Blocks.Remove(_rtbDocument.Blocks.FirstBlock); 
            }
}