[英]Formatting text in a TextBlock
如何在 WPF 應用程序的TextBlock
控件中實現文本格式?
例如:我希望某些單詞為粗體,其他為斜體,有些為不同的顏色,例如以下示例:
我的問題背后的原因是這個實際問題:
lblcolorfrom.Content = "Colour From: " + colourChange.ElementAt(3).Value.ToUpper();
我希望字符串的第二部分為粗體,並且我知道我可以使用兩個控件(標簽、文本塊等),但我寧願不使用,因為已經有大量控件在使用。
您需要使用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>
如果您將粗體作為布爾值(例如),則可以通過轉換器進行綁定。
你可以很容易地在 XAML 中做到這一點:
<TextBlock>
Hello <Bold>my</Bold> faithful <Underline>computer</Underline>.<Italic>You rock!</Italic>
</TextBlock>
有各種Inline
元素可以幫助您,對於最簡單的格式選項,您可以使用Bold
、 Italic
和Underline
:
<TextBlock>
Sample text with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> words.
</TextBlock>
我認為值得注意的是,這些元素實際上只是具有各種屬性設置的Span
元素的簡寫(即:對於Bold
, FontWeight
屬性設置為FontWeights.Bold
)。
這將我們帶到了下一個選項:前面提到的Span
元素。
您可以使用此元素實現與上述相同的效果,但您獲得了更多的可能性; 您可以設置(除其他外) Foreground
或Background
屬性:
<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>
Span
元素還可以包含其他元素,如下所示:
<TextBlock>
<Span FontStyle="Italic">Italic <Span Background="Yellow">text</Span> with some <Span Foreground="Blue">coloring</Span>.</Span>
</TextBlock>
還有另一個元素,它與Span
非常相似,稱為Run
。 Run
不能包含其他內聯元素,而Span
可以,但您可以輕松地將變量綁定到Run
的Text
屬性:
<TextBlock>
Username: <Run FontWeight="Bold" Text="{Binding UserName}"/>
</TextBlock>
此外,如果您願意,您可以從代碼隱藏進行整個格式化:
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.");
從 Charles Petzolds 中查看此示例 Bool Application = Code + markup
//----------------------------------------------
// 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;
}
}
}
一個很好的網站,有很好的解釋:
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 顯示了帶有粗斜體和下划線文本的基本格式。
以下包括 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 顯示了跨度函數及其不同的可能性。
有關詳細說明,請查看網站!
這是我的解決方案......
<TextBlock TextWrapping="Wrap" Style="{DynamicResource InstructionStyle}">
<Run Text="This wizard will take you through the purge process in the correct order." FontWeight="Bold"></Run>
<LineBreak></LineBreak>
<Run Text="To Begin, select" FontStyle="Italic"></Run>
<Run x:Name="InstructionSection" Text="'REPLACED AT RUNTIME'" FontWeight="Bold"></Run>
<Run Text="from the menu." FontStyle="Italic"></Run>
</TextBlock>
我正在學習......所以如果有人對上述解決方案有想法,請分享! :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.