2011-11-03 157 views
3
StringBuilder sb = new StringBuilder(); 
sb.Append(
     string.Format("{0} |{1} ", Name, Value) 
     ); 
Display.Text = sb.ToString(); // Display is a WP7 TextBlock control 

我想將「名稱」設置爲粗體。有可能這樣做嗎?如何使Stringbuilder內容的一部分變爲粗體?

+1

StringBuilder只是幫助你構造一個字符串。它不提供任何格式,除非TextBlock控件接受特殊的格式化字符(我不熟悉WP7 UI編程)。 –

回答

2

你需要把文字變成RichTextBox並有名字作爲一個單獨的RunParagraph在這個例子中從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); 
+0

WP7是否有RTB? –

+0

@HenkHolterman根據MSDN頁面支持 - 有方法等的手機圖標和版本信息狀態「** Windows Phone的Silverlight **支持:Windows Phone OS 7.1」 – ChrisF

+1

@ChrisF:RTB有點沉重的基本要求。 – AnthonyWJones

2

A StringBuilder只包含字符數據,不包含格式。基本上你不能。除非你真的生成html或rtf等

以notepad.exe不具有粗體/斜體/ etc的相同方式。

我不是WP7專家,但也許有一個不同的控制,你可以在這裏使用,更多針對格式化文本。

7

ChrisF提供RichTextBox作爲一個解決方案,但其不太知道簡單的字體變化是簡單的可以使用TextBlock: -

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

我忘記了這一點。這甚至在我的答案! – ChrisF

+0

感謝您的回覆。我會嘗試使用Textblock! – Sri

相關問題