StringBuilder sb = new StringBuilder();
sb.Append(
string.Format("{0} |{1} ", Name, Value)
);
Display.Text = sb.ToString(); // Display is a WP7 TextBlock control
我想把“Name”加粗,这是否可能?
StringBuilder sb = new StringBuilder();
sb.Append(
string.Format("{0} |{1} ", Name, Value)
);
Display.Text = sb.ToString(); // Display is a WP7 TextBlock control
我想把“Name”加粗,这是否可能?
ChrisF提供了RichTextBox
作为解决方案,但较少人知道使用简单的TextBlock
也可以实现简单的字体变化:
myTextBlock.Inlines.Add(new Run() { Text = "Hello " });
myTextBlock.Inlines.Add(new Run() { Text = "World", FontWeight= FontWeights.Bold });
StringBuilder
只包含字符数据,不包含格式。如果你不是在生成html或rtf等,则基本上无法添加格式。就像记事本.exe没有加粗/斜体等一样。我不是WP7专家,但也许有一个不同的控件可以用于此处,更适合格式化文本。RichTextBox
中,并将名称作为单独的Run
放在Paragraph
中,就像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);