How to programmatically install TextBlock Foreground Hex Color

I need to use the foreground color of a text block. Pragmatically.it does not work. Please help me.

I tried this example

txtHome.Foreground = new SolidColorBrush(Colors.Red);

but I want to use Hex color code not to use Color.red etc.

+4
source share
5 answers

Although there are answers that create unnecessary lines, I would suggest just using the most efficient ones:

var brush=new SolidColorBrush(Color.FromArgb(0xFF, 0xD0, 0x20, 0x30));

FromArgb . - , 255/0xFF, . 3 , , , . : "D02030".

, , SolidColorBrush, app.xaml, :

<SolidColorBrush x:Key="myBrush" Color="#D02030" />

:

txtHome.Foreground =  App.Current.Resources["myBrush"] as SolidColorBrush;
+9

:

public class ColorConverter
    {
     public static SolidColorBrush GetColorFromHexa(string hexaColor)
        {
            return new SolidColorBrush(
                Color.FromArgb(
                    Convert.ToByte(hexaColor.Substring(1, 2), 16),
                    Convert.ToByte(hexaColor.Substring(3, 2), 16),
                    Convert.ToByte(hexaColor.Substring(5, 2), 16),
                    Convert.ToByte(hexaColor.Substring(7, 2), 16)
                )
            );
        }
    }

:

txtHome.Foreground = ColorConverter.GetColorFromHexa(("#FFF0F0F0"));
+3

You can use this function to convert the hexadecimal color to a color value, and then you can set it in a text block.

public Color ConvertStringToColor (String hex) {

        hex = hex.Replace("#", "");

        byte a = 255;
        byte r = 255;
        byte g = 255;
        byte b = 255;

        int start = 0;

        //handle ARGB strings (8 characters long)
        if (hex.Length == 8)
        {
            a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
            start = 2;
        }

        //convert RGB characters to bytes
        r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
        g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);
        b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);

        return Color.FromArgb(a, r, g, b);
    }

Color color1 = ConvertStringToColor ("# F0A30A"); txtHome.Foreground = new SolidColorBrush (color1);

+1
source
 txtHome.Foreground = GetColorFromHexa("#FF0000");

 SolidColorBrush GetColorFromHexa(string hexaColor)
        {
            byte r = Convert.ToByte(hexaColor.Substring(1, 2), 16);
            byte g = Convert.ToByte(hexaColor.Substring(3, 2), 16);
            byte b = Convert.ToByte(hexaColor.Substring(5, 2), 16);
            SolidColorBrush soliColorBrush = new SolidColorBrush(Color.FromArgb(0xFF, r, g, b));
            return soliColorBrush;
        }
+1
source

Create the method mentioned below

public SolidColorBrush GetColorFromHexa(string hexaColor)
        {
            return new SolidColorBrush(
                Color.FromArgb(
                    Convert.ToByte(hexaColor.Substring(1, 2), 16),
                    Convert.ToByte(hexaColor.Substring(3, 2), 16),
                    Convert.ToByte(hexaColor.Substring(5, 2), 16),
                    Convert.ToByte(hexaColor.Substring(7, 2), 16)
                )
            );
        }

and use it as

image.Background = GetColorFromHexa("#FF7b9a30");
0
source

Source: https://habr.com/ru/post/1531106/


All Articles