Programmatically set TextBlock Foreground Color

2020-05-19 03:33发布

Is there a way to do this in Windows Phone 7?

I can reference the TextBlock in my C# Code, but I don't know exactly how to then set the foreground color of it.

myTextBlock.Foreground = 
//not a clue...

Thanks

4条回答
我命由我不由天
2楼-- · 2020-05-19 04:09
 textBlock.Foreground = new SolidColorBrush(Colors.White);
查看更多
戒情不戒烟
3楼-- · 2020-05-19 04:10

You could use Brushes.White to set the foreground.

myTextBlock.Foreground = Brushes.White;

The Brushes class is located in System.Windows.Media namespace.

Or, you can press Ctrl+. while the cursor is on the unknown class name to automatically add using directive.

查看更多
Explosion°爆炸
4楼-- · 2020-05-19 04:11

Foreground needs a Brush, so you can use

textBlock.Foreground = Brushes.Navy;

If you want to use the color from RGB or ARGB then

textBlock.Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(100, 255, 125, 35)); 

or

textBlock.Foreground = new System.Windows.Media.SolidColorBrush(Colors.Navy); 

To get the Color from Hex

textBlock.Foreground = new System.Windows.Media.SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFDFD991")); 
查看更多
贼婆χ
5楼-- · 2020-05-19 04:18

To get the Color from Hex.

using System.Windows.Media;

Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991");

and then set the foreground

textBlock.Foreground = new System.Windows.Media.SolidColorBrush(color); 
查看更多
登录 后发表回答