-2

I am creating a Windows store application in C# and XAML (not WPF) using Visual stuio 2013 Professional edition.

I already have an if statement so the program will be able to perform this function

if (TextBoxForRainbow.Text=="rainbow" || TextBoxForRainbow.Text=="Rainbow")
{
    RainbowButton.Opacity = 100;
}

I have explored methods such as: TextBoxForRainbow.Foreground = new SolidColorBrush(color: "red"); but to no avail The link I found talking about this method is here Programmatically set TextBlock Foreground Color

Is there a specific namespace or reference that I need to insert to fulfill this function?

What I want is when the user enters the correct text in the texbox, which in this case is 'rainbow' i would like the text to change to a green colour.

Community
  • 1
  • 1
surjudpphu
  • 53
  • 1
  • 8

1 Answers1

0

In your example, you mentioned that you tried

TextBoxForRainbow.Foreground new SolidColorBrush(color: "red"); 

That won't work, the constructor of the SolidColorBrush expects a Color, not a string.

Here's my reworked example:

XAML

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  <TextBox x:Name='TextBoxForRainbow'
            TextChanged='TextBoxForRainbow_TextChanged'
            Text='Demo' />
</StackPanel>

Code

private void TextBoxForRainbow_TextChanged(object sender, TextChangedEventArgs e) {
  if (String.Equals(TextBoxForRainbow.Text, 
                    "rainbow",
                     StringComparison.CurrentCultureIgnoreCase))
  {
    TextBoxForRainbow.Foreground = new SolidColorBrush(Windows.UI.Colors.Green);
  }
} 

Screenshot

Walt Ritscher
  • 6,977
  • 1
  • 28
  • 35