我如何能改变一个WPF文本框的背景和前景的颜色在c#编程?
当前回答
我认为你是在XAML中创建的文本框?
在这种情况下,您需要给文本框一个名称。然后在后台代码中,您可以使用各种画笔设置Background属性。其中最简单的是SolidColorBrush:
myTextBox.Background = new SolidColorBrush(Colors.White);
其他回答
如果你想使用十六进制颜色设置背景,你可以这样做:
var bc = new BrushConverter();
myTextBox.Background = (Brush)bc.ConvertFrom("#FFXXXXXX");
或者你可以在XAML中设置一个SolidColorBrush资源,然后在后台代码中使用findResource:
<SolidColorBrush x:Key="BrushFFXXXXXX">#FF8D8A8A</SolidColorBrush>
myTextBox.Background = (Brush)Application.Current.MainWindow.FindResource("BrushFFXXXXXX");
textBox1.Background = Brushes.Blue;
textBox1.Foreground = Brushes.Yellow;
WPF前景和背景类型为System.Windows.Media.Brush。你可以像这样设置另一种颜色:
using System.Windows.Media;
textBox1.Background = Brushes.White;
textBox1.Background = new SolidColorBrush(Colors.White);
textBox1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0));
textBox1.Background = System.Windows.SystemColors.MenuHighlightBrush;
我知道这个问题已经在另一个特种部队的帖子中得到了回答。然而,如果你知道十六进制,你也可以这么做。
textBox1.Background = (SolidColorBrush)new BrushConverter().ConvertFromString("#082049");
你可以使用十六进制颜色:
your_contorl.Color = DirectCast(ColorConverter.ConvertFromString("#D8E0A627"), Color)
你看过Color.FromRgb吗?
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 为什么Visual Studio 2015/2017/2019测试运行器没有发现我的xUnit v2测试
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 没有ListBox。SelectionMode="None",是否有其他方法禁用列表框中的选择?
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何在iis7应用程序池中设置。net Framework 4.5版本