Set background color of WPF Textbox in C# code

Sauron picture Sauron · Jun 11, 2009 · Viewed 398.2k times · Source

How can I change the background and foreground colors of a WPF Textbox programmatically in C#?

Answer

Timbo picture Timbo · Jun 11, 2009
textBox1.Background = Brushes.Blue;
textBox1.Foreground = Brushes.Yellow;

WPF Foreground and Background is of type System.Windows.Media.Brush. You can set another color like this:

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;