c# - C# UWP 如何更改 TextBox 的前景
问题描述
如果更改文本,如何更改 TextBox 的前景?例如,我将写“zzz”,前景将更改为“Red”。
<StackPanel Grid.Column="1" Margin="10">
<TextBox Width="200" Text="asdasd" x:Name="qwerty">
</TextBox>
</StackPanel>
解决方案
首先,您需要将Foreground
and添加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);
}
}
推荐阅读
- algorithm - 查找多组数字之间的交集的最有效方法
- javascript - 使用 javascript 从 HTML 日期选择器中获取正确的日期
- r - 为什么运行 `f <- function(x){x} %>% f` 不会引发错误?
- .net - DateTime 格式在 VS 社区版中返回错误的格式,但在专业版中可以正常工作
- rust - Rust 宏的关键字参数
- node.js - 如何向 AWS CodeDeploy 解释如何部署我的 NodeJS 应用程序?
- python - 数据框中的操作总和
- c++ - 是否有一个 STL 算法采用谓词来总结 C++ 中向量的元素
- c++ - 将保存数组元素的指针的地址传递给c ++中的函数时出错
- python - Flask 的 HTML 花括号中的 Python 代码不起作用