McAuley
McAuley

Reputation: 99

Check if a TextBlocks text is a certain Color

Is there anyway to check if a TextBlock's text is a certain Color. What I mean is, is there a way to say:

if(textblocks foreground is blue)
//do stuff

While I'm at it does anyone know how to change a TextBlock's foreground randomly?

Upvotes: 2

Views: 1075

Answers (1)

keyboardP
keyboardP

Reputation: 69372

Assuming you're using a SolidColorBrush, you can try this

SolidColorBrush b = myTextBlock.Foreground as SolidColorBrush;
if (b != null)
{
   if(b.Color == Windows.UI.Colors.Blue)
   {
      //your code
   }
}

To set a random colour, you can choose a random colour to pick and set it. You can use Random as below, or select from KnownColor as described here.

Random rnd = new Random();     

myTextBlock.Foreground = new SolidColorBrush
                   (Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));

Upvotes: 2

Related Questions