简体   繁体   中英

How to make a part of Stringbuilder content bold?

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 ?

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM