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