TextBlock.Foreground 屬性

定義

取得或設定 Brush ,以套用至 TextBlock的文字內容。

public:
 property Brush ^ Foreground { Brush ^ get(); void set(Brush ^ value); };
Brush Foreground();

void Foreground(Brush value);
public Brush Foreground { get; set; }
var brush = textBlock.foreground;
textBlock.foreground = brush;
Public Property Foreground As Brush
<TextBlock Foreground="{StaticResource resourceName}"/>
- or -
<TextBlock Foreground="colorString"/>
- or -
<TextBlock>
  <TextBlock.Foreground>singleBrush</TextBlock.Foreground>
</TextBlock>

屬性值

用來套用至文字內容的筆刷。 預設值是純程式碼檢視方塊中的 Null 筆刷,但預設的文字樣式會針對淺色主題設定為黑色 (,) 或針對 UI 中TextBlock元素的深色主題) 設定為白色 (。

範例

下列 XAML 範例示範如何使用內嵌定義的屬性值 「Maroon」,將 Foreground 屬性設定為純色。 XAML 剖析器會使用此 「Maroon」 值來參考具名色彩 Colors.Maroon,並建立提供運行時間值的 SolidColorBrush 實例。

<TextBlock
  FontSize="32"
  FontWeight="Bold"
  Foreground="Maroon">
  Maroon
</TextBlock>

備註

TextBlock 不是控制項,因此您不會使用範本來變更其外觀。 您可以改用樣式。 較常見的 (和較佳的做法是) 使用 TextBlock 外觀的具名樣式,而不是為每個 TextBlock 實例設定內嵌屬性,特別是在許多元素中使用通用值時。 如需樣式的詳細資訊,請參閱 快速入門:設定控制項的樣式

文字和文字樣式的預設面板取決於使用中主題和其他設定。 使用預設系統設定或使用者喜好設定的主題和各種樣式和行為,都是 TextBlock 前景色彩和其他文字特性的有效執行時間外觀來源。 您可以藉由變更屬性值,或將不同的樣式套用至特定的 TextBlock 實例,來變更這些預設值。 您可以覆寫 App.xaml 中名為 DefaultTextForegroundThemeBrush 的資源,以變更所有預設文字的前景值。

適用於

另請參閱