[英]How to remove RichTextBox's first, blank line?
我在.xaml文件中定義了一個RichTextBox:
<RichTextBox x:Name="logTextBox" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Auto" IsReadOnly="True" FontSize="14" Margin="48,10,49,9" Background="Black">
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
<Style TargetType="ScrollViewer">
<Setter Property="MaxWidth" Value="500" />
</Style>
</RichTextBox.Resources>
</RichTextBox>
我正在這樣使用它:
public void AddLog(string log, Color color)
{
Run run = new Run(log);
run.Foreground = new SolidColorBrush(color);
Paragraph paragraph = new Paragraph(run);
var numberOfBlocks = logTextBox.Document.Blocks.Count;
const int MaxNumberOfBlocks = 100;
if(numberOfBlocks > MaxNumberOfBlocks)
{
logTextBox.Document.Blocks.Remove(logTextBox.Document.Blocks.FirstBlock);
}
logTextBox.Document.Blocks.Add(paragraph);
logTextBox.ScrollToEnd();
}
總體而言,它看起來不錯,並且可以完成我想要做的所有事情,除了一個我無法處理的小細節-它在文本框的開頭添加了一個空行,即:
有沒有什么辦法解決這一問題?
一開始將其刪除
public MainWindow()
{
InitializeComponent();
logTextBox.Document.Blocks.Remove(logTextBox.Document.Blocks.FirstBlock);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.