如何在RTF中获取 RichTextBox 的文本?我试图这样,但财产不存在。

RichTextBox rtb = new RichTextBox();
string s = rtb.Rtf;
有帮助吗?

解决方案

获取用户在RichTextBox内创建的实际XAML:

   TextRange tr = new TextRange(myRichTextBox.Document.ContentStart,
                                myRichTextBox.Document.ContentEnd);
   MemoryStream ms = new MemoryStream();
   tr.Save(ms, DataFormats.Xaml);
   string xamlText = ASCIIEncoding.Default.GetString(ms.ToArray());
编辑:我没有在我面前测试代码,但 TextRange 类型的实例有一个 Save (to stream)方法 DataFormats 参数,可以是 DataFormats.Rtf

其他提示

有2个RichTextBox类,一个来自winforms框架,另一个来自WPF框架:

System.Windows.Controls.RichTextBox wpfBox;
System.Windows.Forms.RichTextBox winformsBox;

只有Winforms RichTextBox具有Rtf属性,另一个具有包含FlowDocument的Document属性。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top