22

In WPF,I can set the background of a stack panel using the below code

stackPanelFlasher.Background = Brushes.Aqua;

How can I set the color as a hex color code for example #C7DFFC?

mateos
  • 1,405
  • 1
  • 17
  • 26
Shyju
  • 214,206
  • 104
  • 411
  • 497

4 Answers4

40
BrushConverter bc = new BrushConverter();  
stackPanelFlasher.Background=  (Brush)bc.ConvertFrom("#C7DFFC"); 

Should do the job. If you want to make it waterproof, better would be

BrushConverter bc = new BrushConverter();  
Brush brush=(Brush)bc.ConvertFrom("#C7DFFC"); 
brush.Freeze();
stackPanelFlasher.Background=brush;

needs fewer resources...

Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36
HCL
  • 36,053
  • 27
  • 163
  • 213
14
stackPanelFlasher.Background = new SolidColorBrush(Color.FromArgb(alpha, red, green, blue));
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
11

I think this sample helps you for xaml solution;

 <Border.Background>
       <LinearGradientBrush EndPoint="1.204,0.5" StartPoint="0.056,0.5">
           <GradientStop Color="#FFFFFFFF" Offset="0" />
           <GradientStop Color="#FFD4D7DB" Offset="1" />
       </LinearGradientBrush>                     
  </Border.Background>
NetSide
  • 3,849
  • 8
  • 30
  • 41
4

The following oneliner should work.

something.Background = (Brush)new BrushConverter().ConvertFrom("#C7DFFC");
nPcomp
  • 8,637
  • 2
  • 54
  • 49