该类包含两个静态方法,第一个参数为需要获取或设置的RichTextBox,第二个参数List<Object> obj为放置内容的List,可存放图片(Image)或文字(Run),若某位置为空时,则添加新的段落(即添加换行),很方便
///<summary>
///解析富文本框
///</summary>
///<param name="ImgPath">图标路径</param>
public class RichTextBoxParseHelper
{
///<summary>
///设置富文本框内内容,obj*可设两种类型数据,Image(图片)和Run(文字),若某位置为空则添加换行
///</summary>
///<param name="mainBox">富文本框</param>
///<param name="obj">填充的内容</param>
static public void SetBoxContent(System.Windows.Controls.RichTextBox mainBox, ref List<Object> obj)
{
FlowDocument document = mainBox.Document;
Paragraph para = new Paragraph();
document.Blocks.Add(para);
for (int index = 0; index < obj.Count; index++)
{
if (obj[index] == null)
{
//添加新段落
para = new Paragraph();
document.Blocks.Add(para);
}
else
{
//文字内容
if (obj[index].GetType().Equals(typeof(Run)))
{
Run newrun = new Run();
newrun.Text = (obj[index] as Run).Text;
para.Inlines.Add(newrun);
}
//图片内容
else if (obj[index].GetType().Equals(typeof(System.Windows.Controls.Image)))
{
System.Windows.Controls.Image newImg = new System.Windows.Controls.Image();
newImg.Source = (obj[index] as System.Windows.Controls.Image).Source;
newImg.Width = 100;
newImg.Height = 100;
//添加图片
InlineUIContainer newContain = new InlineUIContainer();
newContain.Child = newImg;
para.Inlines.Add(newContain);
}
}
}
}
///解析富文本框
///</summary>
///<param name="ImgPath">图标路径</param>
public class RichTextBoxParseHelper
{
///<summary>
///设置富文本框内内容,obj*可设两种类型数据,Image(图片)和Run(文字),若某位置为空则添加换行
///</summary>
///<param name="mainBox">富文本框</param>
///<param name="obj">填充的内容</param>
static public void SetBoxContent(System.Windows.Controls.RichTextBox mainBox, ref List<Object> obj)
{
FlowDocument document = mainBox.Document;
Paragraph para = new Paragraph();
document.Blocks.Add(para);
for (int index = 0; index < obj.Count; index++)
{
if (obj[index] == null)
{
//添加新段落
para = new Paragraph();
document.Blocks.Add(para);
}
else
{
//文字内容
if (obj[index].GetType().Equals(typeof(Run)))
{
Run newrun = new Run();
newrun.Text = (obj[index] as Run).Text;
para.Inlines.Add(newrun);
}
//图片内容
else if (obj[index].GetType().Equals(typeof(System.Windows.Controls.Image)))
{
System.Windows.Controls.Image newImg = new System.Windows.Controls.Image();
newImg.Source = (obj[index] as System.Windows.Controls.Image).Source;
newImg.Width = 100;
newImg.Height = 100;
//添加图片
InlineUIContainer newContain = new InlineUIContainer();
newContain.Child = newImg;
para.Inlines.Add(newContain);
}
}
}
}
///<summary>
///获取富文本框内内容,保存在obj中,obj内共有两种类型数据(Image图片和Run文字),某位置为空时代表换行
///</summary>
///<param name="mainBox">富文本框</param>
///<param name="obj">保存文本框内容</param>
static public void GetBoxContent(System.Windows.Controls.RichTextBox mainBox,ref List<Object> obj)
{
FlowDocument document = mainBox.Document;
int paraBreak = 0;
for (int i = 0; i < document.Blocks.Count; i++)
{
if ((document.Blocks.ElementAt(i)).GetType().Equals(typeof(Paragraph)))
{
Paragraph para = document.Blocks.ElementAt(i) as Paragraph;
//添加回车
if (i != 0 && i != document.Blocks.Count)
{
//添加空数据作为换行标记
obj.Add(null);
}
for (int j = 0; j < para.Inlines.Count; j++)
{
if (para.Inlines.ElementAt(j).GetType().Equals(typeof(InlineUIContainer)))
{
InlineUIContainer childInline = para.Inlines.ElementAt(j) as InlineUIContainer;
obj.Add(childInline.Child);
}
else if (para.Inlines.ElementAt(j).GetType().Equals(typeof(Run)))
{
Run childRun = para.Inlines.ElementAt(j) as Run;
obj.Add(childRun);
}
paraBreak++;
}
else if (para.Inlines.ElementAt(j).GetType().Equals(typeof(Run)))
{
Run childRun = para.Inlines.ElementAt(j) as Run;
obj.Add(childRun);
}
paraBreak++;
}
}
else if ((document.Blocks.ElementAt(i)).GetType().Equals(typeof(BlockUIContainer)))
{
BlockUIContainer uicontainer = document.Blocks.ElementAt(i) as BlockUIContainer;
obj.Add(uicontainer.Child);
}
}
}
}
else if ((document.Blocks.ElementAt(i)).GetType().Equals(typeof(BlockUIContainer)))
{
BlockUIContainer uicontainer = document.Blocks.ElementAt(i) as BlockUIContainer;
obj.Add(uicontainer.Child);
}
}
}
}