在运行时更改Windows 8 XAML TextBox上的背景

时间:2012-10-30 06:34:16

标签: vb.net windows-8 winrt-xaml

如何根据运行时的值更改XAML文本框的背景颜色?最终用户可以自定义这些颜色值。似乎我不能只设置“背景”属性。

    <TextBox Name="nameInput" Width="300" HorizontalAlignment="Left" />

背后的代码如何实现这一目标?这不起作用:

Private Sub NameInput_TextChanged(sender As Object, e As TextChangedEventArgs) Handles nameInput.TextChanged

    Select Case nameInput.Text
        Case "x"
            nameInput.Background = "Red"
        Case "y"
            nameInput.Background = "Green"
    End Select
End Sub

1 个答案:

答案 0 :(得分:1)

您需要指定SolidColorBrush而非Color

Private Sub NameInput_TextChanged(sender As Object, e As TextChangedEventArgs) Handles nameInput.TextChanged

Select Case nameInput.Text
    Case "x"
        nameInput.Background = new SolidColorBrush(Windows.UI.Colors.Red);
    Case "y"
        nameInput.Background = new SolidColorBrush(Windows.UI.Colors.Green);
End Select
End Sub