Set background color of WPF Textbox in C# code

Hi mindstick!

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

Thanks in advance

Last updated:7/17/2013 8:19:08 AM

1 Answers

shreesh chandra shukla
shreesh chandra shukla

Solution!

If you want to set the background using a Hex Color you could do this:

var bc = new BrushConverter();

myTextBox.Background = (Brush)bc.ConvertFrom('#FFXXXXXX');

OR

you could set up a SolidColorBrush resource in xaml, and then use findResource in codebehind

<SolidColorBrush x:Key='BrushFFXXXXXX'>#FF8D8A8A</SolidColorBrush>

myTextBox.Background = (Brush)Application.Current.MainWindow.FindResource('BrushFFXXXXXX');

Answer