c# - 如何删除 RichTextBox 的第一个空行?
问题描述
我在 .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);
}
推荐阅读
- settings - 如何禁用 Thunderbird 自动更新
- webpack - 无法从 aws-sdk-js 访问 QuickSight:QuickSight 不是构造函数
- reactjs - 如何在 material-ui-dropzone 上使用 initialFiles 显示上传的文件?
- javascript - 是否可以检查一个数组是否所有值都为真,有些为真,全部为假
- django - django 如何在管理站点中添加徽标
- javascript - 在 JavaScript 中使用嵌套循环遍历大型二维数组
- html - 溢出出现在移动设备上,但没有出现在本地环境中
- css - 如何修复 SCSS 中的未定义变量错误?
- python - tensorflow对象检测api中的早期停止
- validation - Haskell IO,在同一行中获取两个输入并进行验证