SOS:利用WPF RichTextBox 提取RTF, 莫名丢掉了Underline样式信息

lgg06 2009-06-19 04:26:55
主代码是这样写的:
 richTextBox1.SelectAll();
richTextBox1.Selection.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline);
System.Drawing.FontStyle style = richTextBox1.DrawingFontStyle();
MessageBox.Show(style.ToString()); // <-- result: Underline

string rtf = richTextBox1.RTF();
richTextBox1.LoadFromRTF(rtf);
richTextBox1.SelectAll();
style = richTextBox1.DrawingFontStyle();
MessageBox.Show(style.ToString()); // <-- result: Regular


运行的结果:
第一次显示: Underline
第二次显示: Regular
而RichTextBox里的字体始终是带有下划线的。个人分析的结果为 RichTextBox 提取RTF的字符串丢掉了Underline样式, 那么改如何修正呢?

提取RTF字符串:
public static string RTF(this RichTextBox richTextBox)
{
string rtf = string.Empty;
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
using (MemoryStream ms = new MemoryStream())
{
textRange.Save(ms, System.Windows.DataFormats.Rtf);
ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
rtf = sr.ReadToEnd();
}

return rtf;
}


完整代码我参加本人的Blog:http://www.cnblogs.com/anders06/archive/2009/06/19/1506889.html

...全文
145 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lgg06 2009-06-24
  • 打赏
  • 举报
回复
WPF RichTextBox 没有RTF属性啊, 上面的RTF 跟 LoadFromRTF 是我自己写的扩充方法, RTF是读取,Load是赋值
阿牛138588 2009-06-23
  • 打赏
  • 举报
回复
richTextBox1.LoadFromRTF(rtf);
改成
richTextBox1.RTF=rtf;
lgg06 2009-06-23
  • 打赏
  • 举报
回复
自己顶
feifeiyiwen 2009-06-22
  • 打赏
  • 举报
回复
up
燃烧的荷尔蒙 2009-06-21
  • 打赏
  • 举报
回复
关注~!
V68V6 2009-06-19
  • 打赏
  • 举报
回复
up

jf
angel6709 2009-06-19
  • 打赏
  • 举报
回复
ding

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧