开发者

How do I set formatted text in Silverlight RichTextBox?

开发者 https://www.devze.com 2023-02-05 08:31 出处:网络
How can I make a RichTextBox show a string with format? I\'m using Run but it dosen\'t work: // create a paragraph

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);


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 =)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号