Sri
Sri

Reputation: 177

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 ?

Upvotes: 4

Views: 18041

Answers (3)

AnthonyWJones
AnthonyWJones

Reputation: 189505

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

Upvotes: 7

ChrisF
ChrisF

Reputation: 137168

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

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1063569

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.

Upvotes: 3

Related Questions