Skip to content

Instantly share code, notes, and snippets.

@brunossn
Last active July 16, 2018 17:55
Show Gist options
  • Save brunossn/0dd99083e327533466f12de1320051fc to your computer and use it in GitHub Desktop.
Save brunossn/0dd99083e327533466f12de1320051fc to your computer and use it in GitHub Desktop.
Convert a hexadecimal color string to SolidColorBrush in C#
// Example of usage:
// var color = "#FF0000".ToBrush();
public static class HexToBrushExtension
{
public static SolidColorBrush ToBrush(this string value)
{
var converter = new BrushConverter();
return (SolidColorBrush)converter.ConvertFromString(value);
}
}
@brunossn
Copy link
Author

The same code, now using inline funcion:

public static class HexToBrushExtension
{
        public static SolidColorBrush ToBrush(this string value) =>
            (SolidColorBrush)new BrushConverter().ConvertFromString(value);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment