簡體   English   中英

如何在Silverlight中更改Textblock的背景顏色?

[英]How to change the background color of a Textblock in Silverlight?

我想要一個在黃色背景上有藍色文本的文本塊。 我可以使用“Foreground”屬性設置藍色文本。 但是“背景”不起作用(我猜這太容易了)。

那么最好的方法是將它包裝在具有背景顏色的Rectangle或Canvas中?

而且,有什么我們應該知道的Silverlight能夠理解為什么它們不包含你經常想要設置背景顏色的許多元素的Background屬性嗎?

例如,這會給出錯誤“在Textblock類型中找不到屬性Background”:

<TextBlock 
    Foreground="Blue" 
    Background="Yellow"
        Height="20" 
    HorizontalAlignment="Stretch" 
    Margin="0"
    Test="this is a test"/>

TextBlock派生自FrameworkElement TextBox派生自Control ,派生自FrameworkElement。 Background顏色屬性位於Control中。

在WPF中, TextBlock具有自己的背景屬性

在文本后面添加顏色的最佳方法是將文本放在容器(如Border或Grid)中。 就像是:

<Grid  Background="Yellow" >  
    <TextBlock Foreground="Blue"
               Height="20"
               HorizontalAlignment="Stretch"
               Margin="0" 
               Text="this is a test"/> 
</Grid>
<Border Background="GreenYellow">
    <TextBlock Text="sdfs" Height="60" Width="200"  />
</Border>

由於某些原因,它不在Silverlight中,盡管它在WPF中。 只需將一個邊框包裹起來(它會自動調整大小)。

如果有人發現這個線程谷歌搜索或叮叮當當...

如果您需要背景,請使用自版本3以來的工具包中的標簽控件,我想在版本4中它已經包含在核心中。

請參閱Silverlight中的Label Control

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM