Wpf richtextbox - 在复制/粘贴操作时丢失换行符 [英] Wpf richtextbox - losing line breaks on copy/paste operation

查看:356
本文介绍了Wpf richtextbox - 在复制/粘贴操作时丢失换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个文本控件,它提供单向复制到剪贴板功能,同时还能够格式化特定行和部分行,所以我尝试了RichTextBox。这工作得很好。问题是当我对剪贴板执行复制和粘贴操作时。所有换行符('\ n')都被删除,复制的文本在一行上一起运行。



例如:

这是第1行。

这是第2行。

这是第3行。



成为:

这是第1行。这是第2行。这是第3行。



RichTextBox的文本正在从代码中填充 - 由于所展示内容的动态特性而落后。



I needed a text control that provided a one-way copy to clipboard function while also being able to format specific lines and part of lines so I tried the RichTextBox. This is working pretty well. The problem is when I perform a copy and paste operation to the clipboard. All the the line breaks ('\n') are removed and the copied text is run together on a single line.

For instance:
This is line 1.
This is line 2.
This is line 3.

Becomes:
This is line 1.This is line2.This is line 3.

The text of the RichTextBox is being populated from code-behind due to the very dynamic nature of what is being displayed.

// example
var myDoc = new FlowDocument();

foreach (MyDataObject dObj in dataFromDatabase)
{
  var para = new Paragraph();
  para.Inlines.Add(new Run(dObj.DataPoint1.ToString()));
  if (dObj.DataPoint2 > 3)
  {
    para.Inlines.Add(new Run("\nThis exceeds reporting threshold for blah, blah, blah,...")); // \n or \r it doesn't matter, they are both removed in copy/paste
  }
  // and other lines as required in the program logic
  
  myDoc.Blocks.Add(para);
}

myRichTextBox.Document = myDoc;





我尝试过:



我在互联网上搜索了如何防止这种情况,但我发现的所有内容都是用回车符号('\ r')代替该行但它也没有用。



What I have tried:

I have searched the internet for how to prevent this but all I have found is substituting a carriage return ('\r') for the line break but it also did not work.

推荐答案

我发现了问题,问题不在于'\ r'或'\ n'而在于他们的位置在文中。如果输入\ n \\\ r之类的字符,RichTextBox会开始对行进行双倍间距,并且copy \\\ test操作会将这些行一起运行。现在,如果你改变它们的位置,比如\\\\ n,那么RichTextBox会正确显示它们,copy \paste操作就像它应该的那样工作。我需要做更多的研究,找出重要的原因,但它解决了问题。
I found the problem, the problem is not with '\r' or '\n' but with their position in the text. If you enter the characters like "\n\r", the RichTextBox starts double spacing the lines and a copy\paste operation runs the lines together. Now, if you reverse their positions like "\r\n", the RichTextBox displays them correctly and the copy\paste operation works like it should. I need to do more research to figure out why that matters but it fixed the problem.


这篇关于Wpf richtextbox - 在复制/粘贴操作时丢失换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆