How to make a part of Stringbuilder content bold?

三世轮回 提交于 2020-01-04 02:12:10

问题


StringBuilder sb = new StringBuilder();
sb.Append(
        string.Format("{0} |{1} ", Name, Value)
        );
Display.Text = sb.ToString();  // Display is a WP7 TextBlock control 

I want to make "Name" as bold. Is it possible to do that ?


回答1:


You'll need to put the text into a RichTextBox and have the name as a separate Run in the Paragraph as in this example from the MSDN:

// Create a Run of plain text and some bold text.
Run myRun1 = new Run();
myRun1.Text = "A RichTextBox with ";
Bold myBold = new Bold();
myBold.Inlines.Add("initial content ");
Run myRun2 = new Run();
myRun2.Text = "in it.";

// Create a paragraph and add the Run and Bold to it.
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun1);
myParagraph.Inlines.Add(myBold);
myParagraph.Inlines.Add(myRun2);

// Add the paragraph to the RichTextBox.
MyRTB.Blocks.Add(myParagraph);



回答2:


ChrisF offers the RichTextBox as a solution but its less well known that simple font variation is acheivable with the simple TextBlock:-

myTextBlock.Inlines.Add(new Run() { Text = "Hello " });
myTextBlock.Inlines.Add(new Run() { Text = "World", FontWeight= FontWeights.Bold });



回答3:


A StringBuilder only contains character data, not formatting. You can't, basically. Unless you are actually generating html or rtf etc.

In the same way that notepad.exe doesn't have bold/italics/etc.

I'm not a WP7 expert, but maybe there is a different control you can use here, more aimed at formatted text.



来源:https://stackoverflow.com/questions/8002687/how-to-make-a-part-of-stringbuilder-content-bold

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