Programmatically set TextBlock Foreground Color

C#Windows Phone-7ColorsTextblock

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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

Solution 3 - C#

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.

Solution 4 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser818700View Question on Stackoverflow
Solution 1 - C#Diana MikhasyovaView Answer on Stackoverflow
Solution 2 - C#Kishore KumarView Answer on Stackoverflow
Solution 3 - C#AgentFireView Answer on Stackoverflow
Solution 4 - C#Kishore KumarView Answer on Stackoverflow