简体   繁体   English

以编程方式设置TextBlock前景色

[英]Programmatically set TextBlock Foreground Color

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 谢谢

 textBlock.Foreground = new SolidColorBrush(Colors.White);

Foreground needs a Brush, so you can use 前景需要刷子,所以你可以使用

textBlock.Foreground = Brushes.Navy;

If you want to use the color from RGB or ARGB then 如果你想使用RGBARGB的颜色

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")); 

You could use Brushes.White to set the foreground. 您可以使用Brushes.White设置前景。

myTextBlock.Foreground = Brushes.White;

The Brushes class is located in System.Windows.Media namespace. Brushes类位于System.Windows.Media命名空间中。

Or, you can press Ctrl + . 或者,您可以按Ctrl + while the cursor is on the unknown class name to automatically add using directive. 而光标在未知的类名上,以自动添加using指令。

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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM