如何根据运行时的值更改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
答案 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