Creating SolidColorBrush from hex color value

后端 未结 6 1514
挽巷
挽巷 2020-12-12 14:59

I want to create SolidColorBrush from Hex value such as #ffaacc. How can I do this?

On MSDN, I got :

SolidColorBrush mySolidColorBrush = new SolidCol         


        
6条回答
  •  一个人的身影
    2020-12-12 15:02

    If you don't want to deal with the pain of the conversion every time simply create an extension method.

    public static class Extensions
    {
        public static SolidColorBrush ToBrush(this string HexColorString)
        {
            return (SolidColorBrush)(new BrushConverter().ConvertFrom(HexColorString));
        }    
    }
    

    Then use like this: BackColor = "#FFADD8E6".ToBrush()

    Alternately if you could provide a method to do the same thing.

    public SolidColorBrush BrushFromHex(string hexColorString)
    {
        return (SolidColorBrush)(new BrushConverter().ConvertFrom(hexColorString));
    }
    
    BackColor = BrushFromHex("#FFADD8E6");
    

提交回复
热议问题