C# 以编程方式设置 TextBlock 前景色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12727491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically set TextBlock Foreground Color
提问by
Is there a way to do this in Windows Phone 7?
有没有办法在 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.
我可以在我的 C# 代码中引用 TextBlock,但我不知道如何设置它的前景色。
myTextBlock.Foreground =
//not a clue...
Thanks
谢谢
采纳答案by Diana Mikhasyova
textBlock.Foreground = new SolidColorBrush(Colors.White);
回答by AgentFire
You could use Brushes.Whiteto set the foreground.
你可以Brushes.White用来设置前景。
myTextBlock.Foreground = Brushes.White;
The Brushesclass is located in System.Windows.Medianamespace.
本Brushes类位于System.Windows.Media命名空间。
Or, you can press Ctrl+.while the cursor is on the unknown class name to automatically add usingdirective.
或者,您可以在光标位于未知类名称上时按Ctrl+.以自动添加using指令。
回答by Kishore Kumar
Foreground needs a Brush, so you can use
前景需要一个画笔,所以你可以使用
textBlock.Foreground = Brushes.Navy;
If you want to use the color from RGBor ARGBthen
如果你想使用来自RGB或ARGB的颜色,那么
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"));
回答by Kishore Kumar
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);

