Anoushka Seechurn
Anoushka Seechurn

Reputation: 2256

Set color of a textbox programatically in a WPF application

In trying to set the color of a text box in my WPF application I did

enter image description here

Why do I get that error ?

And how to rectify it ?

Upvotes: 1

Views: 584

Answers (2)

Grant Winney
Grant Winney

Reputation: 66439

It's telling you right in the error. The Background property is of type System.Windows.Media.Brush, not System.Windows.Media.Color, so you can't assign a Color to it.

Pass a Brush into your method instead of a Color, and assign that to the Background property.

public void addToStackPanel(string argBuiltAlarm, Brush brush)
{
    ...
    TextBox textBox = new TextBox { Background = brush };
    ...

Upvotes: 5

Monika
Monika

Reputation: 2210

textBox1.Background = Brushes.Blue;
textBox1.Foreground = Brushes.Yellow;

You can do like this.

Upvotes: -1

Related Questions