WPF
Textbox
C#
background color
programming

Set background color of WPF Textbox in C code

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In WPF, a TextBox background is controlled by its Background property, which expects a Brush. If you want to change the color from C# code, the normal approach is to assign either a predefined brush from Brushes or a SolidColorBrush you create yourself.

The Simplest Programmatic Change

If you already have a TextBox instance, the direct code is straightforward:

csharp
using System.Windows.Media;

MyTextBox.Background = Brushes.LightYellow;

That works because Brushes.LightYellow returns a reusable SolidColorBrush.

If you need a custom color rather than a predefined one, create a brush explicitly:

csharp
1using System.Windows.Media;
2
3var brush = new SolidColorBrush(Color.FromRgb(255, 240, 200));
4MyTextBox.Background = brush;

Both examples are valid. The only difference is whether the color comes from WPF's predefined brush set or from your own RGB values.

A Complete Window Example

Here is a minimal code-behind example that changes the background when a button is clicked.

xml
1<Window x:Class="WpfApp1.MainWindow"
2        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4        Title="Demo" Height="180" Width="320">
5    <StackPanel Margin="20">
6        <TextBox x:Name="MyTextBox" Text="Hello" Margin="0,0,0,12" />
7        <Button Content="Highlight" Click="Highlight_Click" />
8    </StackPanel>
9</Window>
csharp
1using System.Windows;
2using System.Windows.Media;
3
4namespace WpfApp1;
5
6public partial class MainWindow : Window
7{
8    public MainWindow()
9    {
10        InitializeComponent();
11    }
12
13    private void Highlight_Click(object sender, RoutedEventArgs e)
14    {
15        MyTextBox.Background = Brushes.LightGreen;
16    }
17}

This is the most common pattern in smaller WPF applications.

Using A Dynamic Color Choice

Sometimes the color depends on validation state or application logic. In that case, compute the brush in code.

csharp
1using System.Windows.Media;
2
3private void UpdateBackground(bool hasError)
4{
5    MyTextBox.Background = hasError
6        ? Brushes.MistyRose
7        : Brushes.White;
8}

This is useful when the UI should react to user input, parsing results, or remote status values.

If you need more exact colors, use ColorConverter or FromArgb.

csharp
1using System.Windows.Media;
2
3var color = (Color)ColorConverter.ConvertFromString("#FFDDEEFF");
4MyTextBox.Background = new SolidColorBrush(color);

Remember That WPF Uses Brush, Not Color

A common beginner mistake is trying to assign a Color directly:

csharp
// wrong
// MyTextBox.Background = Colors.Red;

That fails because Background expects a Brush, not a Color. Wrap the color in SolidColorBrush or use a member from Brushes.

csharp
MyTextBox.Background = new SolidColorBrush(Colors.Red);

That small type distinction explains many compile errors around WPF styling code.

UI Thread Requirements

WPF controls must be updated on the UI thread. If the background change happens after background work, marshal back to the dispatcher.

csharp
1using System.Threading.Tasks;
2using System.Windows;
3using System.Windows.Media;
4
5private async Task LoadDataAsync()
6{
7    await Task.Run(() => System.Threading.Thread.Sleep(500));
8
9    Application.Current.Dispatcher.Invoke(() =>
10    {
11        MyTextBox.Background = Brushes.LightBlue;
12    });
13}

If you update MyTextBox.Background from a worker thread directly, WPF can throw a cross-thread access exception.

Code-Behind Versus Styles

Changing the background in C# is fine for event-driven logic, but not always the best long-term styling approach. If the color is purely a visual rule based on control state, a XAML style or trigger is often cleaner.

Still, code-behind is appropriate when:

  • the color depends on runtime data not easily expressed in XAML
  • a one-off event changes the control state
  • you are building or modifying controls dynamically

So the right answer is not that code is wrong. It is that code should be used for behavioral logic, while general styling rules are often better expressed declaratively.

Common Pitfalls

  • Assigning Colors.Red directly instead of a Brush.
  • Updating the control from a background thread.
  • Creating many custom brushes when a predefined brush from Brushes would do.
  • Using code-behind for styling rules that belong in a reusable XAML style.
  • Forgetting that disabled controls may render with theme-specific visuals that affect the perceived color.

Summary

  • Set a WPF TextBox background by assigning a Brush to Background.
  • Use Brushes.SomeColor for simple cases.
  • Use new SolidColorBrush(...) for custom colors.
  • Update WPF controls on the UI thread.
  • Prefer XAML styles for reusable visual rules, but use C# when the color depends on runtime behavior.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions