繁体   English   中英

如何使Stringbuilder内容的一部分变为粗体?

[英]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 

我想把“名字”变成粗体。 有可能吗?

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

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

StringBuilder只包含字符数据,而不是格式。 基本上你不能。 除非您实际生成html或rtf等。

与notepad.exe没有粗体/斜体/等的方式相同。

我不是WP7专家,但也许你可以在这里使用不同的控件,更多的是针对格式化的文本。

您需要将文本放入RichTextBox并在Paragraph中将该名称作为单独的Run ,如本示例中的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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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