为什么RichTextBox在文档开始之前总是包含额外的字符?

时间:2018-05-03 15:15:31

标签: c# wpf

我从一个空白的RichTextBox开始

    <RichTextBox x:Name="textBox" HorizontalAlignment="Left" Height="234" Margin="10,75,0,0" VerticalAlignment="Top" Width="214" PreviewMouseLeftButtonUp="textBox_PreviewMouseLeftButtonUp">
    </RichTextBox>

然后我加载一个文件,读取所有文本,然后将其添加到RichTextBox中,就像这样

using (StreamReader sr = new StreamReader(@"D:\Text.txt"))
{
    string fullText = sr.ReadToEnd();

    textBox.Document.Blocks.Add(new Paragraph(new Run(fullText)));
}

当我检查我插入的fullText时,看起来像这样......立即以字符E

开头

enter image description here

但是在渲染到控件之后,我选择了文本开头的一部分,但是在第一个字符之前似乎有一个回车换行符。

enter image description here

enter image description here

所以我的问题是它总是插入这些字符吗?

有没有办法删除此行为,因此它不会在文档的第一个字符之前插入任何内容?

1 个答案:

答案 0 :(得分:2)

您的RichTextBox中已有一个Block。 您可以使用<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <style> .block{ height: 200px; background: lavender; padding: 20px; text-align: center; font-size: 20px; color: #000; } .push_down { margin-bottom: 30px; } .hidden { display: none; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12"> <div class="col-md-offset-3 col-md-6 col-sm-8"> <div class="parent"> <div class="panel-primary"> <div class="panel-heading push_down"> <div class="block"> BLOCK A </div> </div> <div class="panel-heading push_down"> <div class="block"> BLOCK B </div> </div> <div class="panel-heading push_down"> <div class="block"> Here </div> </div> <div class="panel-heading push_down"> <div class="block"> Where </div> </div> <div class="panel-heading push_down"> <div class="block"> Dark </div> </div> <div class="panel-heading push_down"> <div class="block"> BLOCK B </div> </div> </div> </div> <div class="parent"> <div class="panel-primary"> <div class="panel-heading push_down"> <div class="block"> BLOCK AA </div> </div> <div class="panel-heading push_down"> <div class="block"> BLOCK BB </div> </div> </div> </div> </div> </div> </div> </body> </html>将其删除。 你的第一个新段落现在没有空间。 如果段落在另一个元素之后,段落会在它们之上增加空间。