我如何能改变一个WPF文本框的背景和前景的颜色在c#编程?


当前回答

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;

其他回答

你可以使用十六进制颜色:

your_contorl.Color = DirectCast(ColorConverter.ConvertFromString("#D8E0A627"), Color)
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");

我认为你是在XAML中创建的文本框?

在这种情况下,您需要给文本框一个名称。然后在后台代码中,您可以使用各种画笔设置Background属性。其中最简单的是SolidColorBrush:

myTextBox.Background = new SolidColorBrush(Colors.White);

你看过Color.FromRgb吗?