[英]How to update background color of the border based on value in textblock in runtime in Silverlight?
[英]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中。
在文本后面添加顏色的最佳方法是將文本放在容器(如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中。 只需將一個邊框包裹起來(它會自動調整大小)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.