How can I change the background and foreground colors of a WPF Textbox programmatically in C#?
This question is related to
c#
.net
wpf
background-color
I take it you are creating the TextBox in XAML?
In that case, you need to give the text box a name. Then in the code-behind you can then set the Background property using a variety of brushes. The simplest of which is the SolidColorBrush:
myTextBox.Background = new SolidColorBrush(Colors.White);
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 the code-behind:
<SolidColorBrush x:Key="BrushFFXXXXXX">#FF8D8A8A</SolidColorBrush>
myTextBox.Background = (Brush)Application.Current.MainWindow.FindResource("BrushFFXXXXXX");
You can convert hex to RGB:
string ccode = "#00FFFF00";
int argb = Int32.Parse(ccode.Replace("#", ""), NumberStyles.HexNumber);
Color clr = Color.FromArgb(argb);
I know this has been answered in another SOF post. However, you could do this if you know the hexadecimal.
textBox1.Background = (SolidColorBrush)new BrushConverter().ConvertFromString("#082049");
You can use hex colors:
your_contorl.Color = DirectCast(ColorConverter.ConvertFromString("#D8E0A627"), Color)
Have you taken a look at Color.FromRgb
?
Source: Stackoverflow.com