How do I set formatted text in Silverlight RichTextBox?

社会主义新天地 提交于 2019-12-12 05:00:49

问题


How can I make a RichTextBox show a string with format?

I'm using Run but it dosen't work:

 // create a paragraph
 Paragraph prgParagraph = new Paragraph();
 prgParagraph.FontFamily = new FontFamily("Comic Sans MS");

 // create some text, and add it to the paragraph
 Run rnMyText = new Run();
 rnMyText.Text = w.meaning;

 prgParagraph.Inlines.Add(rnMyText);

 rtxtMeaning.Blocks.Add(prgParagraph);

回答1:


I know that this question is a couple years old, but I had the same question and here's what I came up with. I've tested it a few times with my Silverlight 5 project and it works for me.

public static void setRtf(ref RichTextBox rtfBox, string text)
{
     Paragraph p = new Paragraph();
     p.FontFamily = rtfBox.FontFamily;
     Run pTxt = new Run();
     pTxt.Text = text;
     p.Inlines.Add(pTxt);
     rtfBox.Blocks.Clear();
     rtfBox.Blocks.Add(p);
}

make sure that when you call the method you use the ref keyword for your RichTextBox object and you're good to go =)



来源:https://stackoverflow.com/questions/4740674/how-do-i-set-formatted-text-in-silverlight-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!