如何在C#中以编程方式更改WPF文本框的背景色和前景色?
发布于 2009-06-11 08:06:02
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;
发布于 2011-11-07 16:09:06
如果你想使用十六进制颜色设置背景,你可以这样做:
var bc = new BrushConverter();
myTextBox.Background = (Brush)bc.ConvertFrom("#FFXXXXXX");
或者,您可以在XAML中设置SolidColorBrush资源,然后在代码隐藏中使用findResource:
#FF8D8A8A
myTextBox.Background = (Brush)Application.Current.MainWindow.FindResource("BrushFFXXXXXX");
发布于 2009-06-11 08:06:14
我猜你是在用XAML创建TextBox吧?
在这种情况下,您需要为文本框指定一个名称。然后,在代码隐藏中,您可以使用各种画笔设置背景属性。其中最简单的是SolidColorBrush:
myTextBox.Background = new SolidColorBrush(Colors.White);
https://stackoverflow.com/questions/979876
复制相似问题