[英]C# UWP How can I change foreground of TextBox
如果文本更改,如何更改 TextBox 的前景? 例如,我會寫“zzz”,前景會在“Red”上發生變化。
<StackPanel Grid.Column="1" Margin="10">
<TextBox Width="200" Text="asdasd" x:Name="qwerty">
</TextBox>
</StackPanel>
首先需要將Foreground
和TextChanged
添加到 TextBox:
<TextBox Foreground="{DynamicResource txtColor}" Width="200" Text="asdasd" x:Name="qwerty" TextChanged="TextBox1_TextChanged">
</TextBox>
然后您可以在 TextChanged 事件中動態更改文本框的前景,如下所示:
private void TextBox1_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
if(textBox.Text == "zzz")
{
Application.Current.Resources["txtColor"] = new SolidColorBrush(Colors.Red);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.