将格式化字符串设置为WPF中的RichTextBox

时间:2014-04-14 19:38:39

标签: wpf richtextbox

如何将格式化字符串设置为RichTextBox控件,使其显示格式化文本,而不是纯文本。

字符串示例:

{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1046{\fonttbl{\f0\fnil\fcharset0   Calibri;}}
{\colortbl ;\red255\green0\blue0;}
{\*\generator Riched20 6.3.9600}\viewkind4\uc1 
\pard\sa200\sl276\slmult1\tx568\f0\fs22\lang22 This is an \cf1\ul example\cf0\ulnone\par
}

1 个答案:

答案 0 :(得分:1)

您可以使用RichTextBox.Selection.Load格式Rtf方法设置文字:

var text = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1046{\fonttbl{\f0\fnil\fcharset0   Calibri;}}{\colortbl ;\red255\green0\blue0;}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\sa200\sl276\slmult1\tx568\f0\fs22\lang22 This is an \cf1\ul example\cf0\ulnone\par}";

using (MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(text)))
{
    rtb.Selection.Load(stream, DataFormats.Rtf);
}