【问题标题】:Change foreground text of texbox更改文本框的前景文本
【发布时间】:2015-02-20 00:42:41
【问题描述】:

我正在使用 Visual stuio 2013 专业版在 C# 和 XAML(不是 WPF)中创建一个 Windows 商店应用程序。

我已经有一个 if 语句,所以程序将能够执行这个功能

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

我探索了以下方法: TextBoxForRainbow.Foreground = new SolidColorBrush(color: "red");但无济于事 我发现谈论这种方法的链接在这里Programmatically set TextBlock Foreground Color

是否需要插入特定的命名空间或引用来实现此功能?

我想要的是当用户在 texbox 中输入正确的文本时,在这种情况下是“彩虹”,我希望文本变为绿色。

【问题讨论】:

  • 您正在构建 Windows 应用商店应用程序吗?
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 是的一个 Windows 商店应用程序 :)
  • 您应该更具体地了解您正在开发的平台。

标签: c# xaml windows-store-apps


【解决方案1】:

在你的例子中,你提到你尝试过

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

这行不通,SolidColorBrush 的构造函数需要 Color,而不是字符串。

这是我修改后的示例:

XAML

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

代码

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

【讨论】:

    猜你喜欢
    • 2011-04-27
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 2013-01-29
    • 2013-06-03
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多