2011-03-10 94 views
80

如何在WPF應用程序中實現TextBlock控件中文本的格式化?格式化TextBlock中的文本

如:我想有大膽,別人斜體某些詞,還有一些不同的顏色,這樣的例子:

enter image description here

背後我的問題的原因是這個實際的問題:

lblcolorfrom.Content = "Colour From: " + colourChange.ElementAt(3).Value.ToUpper(); 

我想字符串的第二部分是勇敢的,我知道,我可以用兩個控件(標籤,的TextBlocks等),但我寧願不要,因爲控件的大量已正在使用。

回答

106

您需要使用Inlines

<TextBlock.Inlines> 
    <Run FontWeight="Bold" FontSize="14" Text="This is WPF TextBlock Example. " /> 
    <Run FontStyle="Italic" Foreground="Red" Text="This is red text. " /> 
</TextBlock.Inlines> 

隨着結合:

<TextBlock.Inlines> 
    <Run FontWeight="Bold" FontSize="14" Text="{Binding BoldText}" /> 
    <Run FontStyle="Italic" Foreground="Red" Text="{Binding ItalicText}" /> 
</TextBlock.Inlines> 

您還可以綁定其他屬性:

<TextBlock.Inlines> 
    <Run FontWeight="{Binding Weight}" 
     FontSize="{Binding Size}" 
     Text="{Binding LineOne}" /> 
    <Run FontStyle="{Binding Style}" 
     Foreground="Binding Colour}" 
     Text="{Binding LineTwo}" /> 
</TextBlock.Inlines> 

,如果您有您可以通過轉換器綁定粗體作爲布爾(說)。

37

退房從查爾斯Petzolds布爾應用=代碼+標記

//---------------------------------------------- 
// FormatTheText.cs (c) 2006 by Charles Petzold 
//---------------------------------------------- 
using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Documents; 

namespace Petzold.FormatTheText 
{ 
    class FormatTheText : Window 
    { 
     [STAThread] 
     public static void Main() 
     { 
      Application app = new Application(); 
      app.Run(new FormatTheText()); 
     } 
     public FormatTheText() 
     { 
      Title = "Format the Text"; 

      TextBlock txt = new TextBlock(); 
      txt.FontSize = 32; // 24 points 
      txt.Inlines.Add("This is some "); 
      txt.Inlines.Add(new Italic(new Run("italic"))); 
      txt.Inlines.Add(" text, and this is some "); 
      txt.Inlines.Add(new Bold(new Run("bold"))); 
      txt.Inlines.Add(" text, and let's cap it off with some "); 
      txt.Inlines.Add(new Bold(new Italic (new Run("bold italic")))); 
      txt.Inlines.Add(" text."); 
      txt.TextWrapping = TextWrapping.Wrap; 

      Content = txt; 
     } 
    } 
} 
77

這個例子中,您可以在XAML中很輕鬆地做到這一點:

<TextBlock> 
    Hello <Bold>my</Bold> faithful <Underline>computer</Underline>.<Italic>You rock!</Italic> 
</TextBlock> 
+0

太棒了!我不知道XAML支持這樣的構造。 – 2012-10-27 17:44:18

+3

是否支持綁定? – 2012-12-18 14:11:35

+7

@ArsenMkrt怎麼樣: Aetherix 2013-02-25 21:40:04

34

有各種Inline元素,可以幫助你,對於最簡單的格式化選項,您可以使用Bold,ItalicUnderline

<TextBlock> 
    Sample text with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> words. 
</TextBlock> 

enter image description here

我認爲這是值得注意的,這些元素實際上只是用於設置各種屬性Span元素速記(即:爲BoldFontWeight屬性設置爲FontWeights.Bold)。

這給我們帶來了我們的下一個選擇:前面提到的Span元素。

您可以使用上述元素獲得相同的效果,但您有更多的可能性;可以設置(等等)的ForegroundBackground屬性:

<TextBlock> 
    Sample text with <Span FontWeight="Bold">bold</Span>, <Span FontStyle="Italic">italic</Span> and <Span TextDecorations="Underline">underlined</Span> words. <Span Foreground="Blue">Coloring</Span> <Span Foreground="Red">is</Span> <Span Background="Cyan">also</Span> <Span Foreground="Silver">possible</Span>. 
</TextBlock> 

enter image description here

Span元件還可以包含其它元素是這樣的:

<TextBlock> 
    <Span FontStyle="Italic">Italic <Span Background="Yellow">text</Span> with some <Span Foreground="Blue">coloring</Span>.</Span> 
</TextBlock> 

enter image description here

有是另一個元素,與非常相似3210,它被稱爲Run。該Run不能包含其他內聯元素而Span可以,但你可以輕鬆地將變量綁定到RunText屬性:

<TextBlock> 
    Username: <Run FontWeight="Bold" Text="{Binding UserName}"/> 
</TextBlock> 

enter image description here

此外,您還可以做從代碼 - 全格式後面如果你喜歡:

TextBlock tb = new TextBlock(); 
tb.Inlines.Add("Sample text with "); 
tb.Inlines.Add(new Run("bold") { FontWeight = FontWeights.Bold }); 
tb.Inlines.Add(", "); 
tb.Inlines.Add(new Run("italic ") { FontStyle = FontStyles.Italic }); 
tb.Inlines.Add("and "); 
tb.Inlines.Add(new Run("underlined") { TextDecorations = TextDecorations.Underline }); 
tb.Inlines.Add("words."); 
4

一個很好的網站,具有良好的解釋:

http://www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/

這裏的作者給你很好的例子,你正在尋找什麼!全部測試該網站是偉大的研究材料,再加上它覆蓋選項很大,你在WPF

編輯有

有不同的方法來格式化文本。一個基本的格式(最簡單的在我看來):

<TextBlock Margin="10" TextWrapping="Wrap"> 
        TextBlock with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> text. 
    </TextBlock> 

示例1顯示了大膽Itallic並強調文本基本格式。

繼包括SPAN方法,這樣,你車的亮點文本:

<TextBlock Margin="10" TextWrapping="Wrap"> 
        This <Span FontWeight="Bold">is</Span> a 
        <Span Background="Silver" Foreground="Maroon">TextBlock</Span> 
        with <Span TextDecorations="Underline">several</Span> 
        <Span FontStyle="Italic">Span</Span> elements, 
        <Span Foreground="Blue"> 
          using a <Bold>variety</Bold> of <Italic>styles</Italic> 
        </Span>. 
    </TextBlock> 

示例2顯示了跨職能和與它不同的可能性。

對於詳細的解釋檢查網站!

Examples

+0

雖然這種鏈路可以回答的問題,最好是包括的主要部分這裏的答案和提供的鏈接供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/12244274) – 2016-05-04 17:28:00

+1

@Mogsdad編輯帖子,以便顯示代碼示例 – Giellez 2016-05-04 17:57:42

+0

@RichardSlater編輯帖子,以便顯示代碼示例 – Giellez 2016-05-04 17:58:18