我希望能够将文本直接粘贴到RichTextBox控件中,同时保留标签,换行符和换行符。
不幸的是,当从剪贴板粘贴文本(Ctrl-V)时,'\ r'字符被放置在Run元素的Text属性中,而不是被转换为<LineBreak />
元素。
所以,鉴于粘贴的文字:
Et harum quidem rerum facilis est et expedita distinctio。
Nam libero tempore,cum soluta nobis est eligendi optio cumque nihil 阻碍减去id quod maxime 安德特·贝弗雷·布里米斯 voluptas assumenda est,omnis dolor repellendus。
和...
<RichTextBox x:Name="rtbNoteText" BorderBrush="Gray" xml:space="preserve" AcceptsReturn="True" TextWrapping="Wrap"
BorderThickness="1" HorizontalAlignment="Stretch" Grid.Row="1"
VerticalAlignment="Stretch" Margin="30,0,30,30" FontSize="14"
VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Auto" />
private void btnSaveNote_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(rtbNoteText.Xaml)) {
string contents = rtbNoteText.Xaml;
}
}
rtbNoteText.Xaml的内容是:
<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">
<Paragraph FontSize=\"14\" FontFamily=\"Portable User Interface\" Foreground=\"#FF000000\" FontWeight=\"Normal\" FontStyle=\"Normal\" FontStretch=\"Normal\" TextAlignment=\"Left\">
<Run Text=\"Et harum quidem rerum facilis est et expedita distinctio. \r\rNam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.\" />
</Paragraph>
</Section>
rtbNoteText.Replace("\r","<LineBreak />")
不会这样做,因为换行符嵌入在Run元素Text Property中。
是否有一种标准的方法可以解决这个问题?:
<Section>
<Paragragh>
<Run>...</Run>
<LineBreak /><LineBreak />
<Run>...</Run>
</Paragraph>
</Section>
答案 0 :(得分:0)
您可以手动构建Paragraph对象,方法是为文本的每一行添加一个对象,以及何时需要换行符。您可以使用String.Split方法将文本拆分为行。
以下是一个例子:
string text = "Et harum quidem rerum facilis est et expedita distinctio.\r\r" +
"Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod\r" +
"maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.";
// I guess this how you are updating the textbox right now
//richTextBox.Selection.Text = text;
Paragraph paragraph = new Paragraph();
string[] lines = text.Split(new char[] {'\r'});
for (int i = 0; i < lines.Length; i++)
{
paragraph.Inlines.Add(new Run() { Text = lines[i] });
if (i < lines.Length - 1) paragraph.Inlines.Add(new LineBreak());
}
richTextBox.Blocks.Add(paragraph);
它应该通过richTextBox.Xaml属性返回以下xaml:
<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Paragraph FontSize="11" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left">
<Run Text="Et harum quidem rerum facilis est et expedita distinctio." />
<LineBreak />
<Run />
<LineBreak />
<Run Text="Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod" />
<LineBreak />
<Run Text="maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus." />
</Paragraph>
</Section>
希望这有帮助,尊重