如何使StringBuilder内容的一部分加粗?

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

我想把“Name”加粗,这是否可能?


1
StringBuilder只是帮助您构建字符串。它不提供任何格式化,除非TextBlock控件接受特殊的格式化字符(我不熟悉WP7 UI编程)。 - Eric J.
3个回答

7

ChrisF提供了RichTextBox作为解决方案,但较少人知道使用简单的TextBlock也可以实现简单的字体变化:

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

我忘记了那个。它甚至在我的答案里! - ChrisF
谢谢回复。我会尝试使用Textblock! - Sri

3
一般来说,StringBuilder 只包含字符数据,不包含格式。如果你不是在生成html或rtf等,则基本上无法添加格式。就像记事本.exe没有加粗/斜体等一样。我不是WP7专家,但也许有一个不同的控件可以用于此处,更适合格式化文本。

2
您需要将文本放入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);

据MSDN页面显示,它得到了支持——其方法等具有电话图标,并且版本信息表明:“Silverlight for Windows Phone Supported in: Windows Phone OS 7.1”。@HenkHolterman - ChrisF
1
@ChrisF:RTB对于基本需求来说有些过于繁重。 - AnthonyWJones

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接