wpfbrushescolor-codes

Setting background of panel with custom color code


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?


Solution

  • 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...