Setting background of panel with custom color code
Asked Answered
Z

4

21

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?

Zarger answered 14/7, 2010 at 13:33 Comment(0)
I
41
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...

Ingenerate answered 14/7, 2010 at 13:39 Comment(0)
A
14
stackPanelFlasher.Background = new SolidColorBrush(Color.FromArgb(alpha, red, green, blue));
Antitoxic answered 14/7, 2010 at 13:37 Comment(0)
D
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>
Demob answered 14/7, 2010 at 13:39 Comment(0)
V
3

The following oneliner should work.

something.Background = (Brush)new BrushConverter().ConvertFrom("#C7DFFC");
Versify answered 16/1, 2019 at 21:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.