2015-04-05 29 views
0

我试图在总行数超过预定数目的条目时删除段落的第一行。这是一种聊天窗口,我不想一次显示太多的行。系统地删除第一行

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. 
      } 
} 

不确定最简单的方法来做到这一点,迄今为止我尝试过的一切都没有奏效。

+0

你想删除只是第一行? – 2015-04-05 20:27:34

+0

是的,每次添加新条目(例如)时,都会添加一个新条目(例如),我想删除最旧的条目,以便显示最多50行。 – CircuitSix 2015-04-05 20:30:13

+0

你期望它做什么?什么是“入口”?它有什么作用?实际计数是多少? – CodeCaster 2015-04-05 20:30:20

回答

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); 
      } 
} 
0

建议您使用List verus数组。它给你一些你需要的功能。

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

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

     return paragraph; 
    } 

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