wpf Foreground赋值
时间: 2023-08-22 17:04:24 浏览: 41
在WPF中,可以使用Foreground属性设置控件的前景色,包括文本、图标等颜色。Foreground属性可以接受Brush类型的值,包括SolidColorBrush、LinearGradientBrush、RadialGradientBrush、ImageBrush等。以下是一些示例:
```xaml
<TextBlock Foreground="Red" Text="This is red text." />
<TextBlock Foreground="#FF0000FF" Text="This is blue text." />
<TextBlock>
<TextBlock.Foreground>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="0" Color="Red" />
<GradientStop Offset="1" Color="Blue" />
</LinearGradientBrush>
</TextBlock.Foreground>
This is gradient text.
</TextBlock>
```
在代码中设置Foreground属性时,需要使用Brush类型的值。例如:
```c#
textBlock.Foreground = Brushes.Red;
```
以上代码将文本块的前景色设置为红色。需要注意的是,如果前景色和背景色相同,可能会导致文本不可见。因此,在设置前景色时,应该确保其和背景色不同。