private SolidColorBrush GetColorFromString(string color)
{
if (color.StartsWith("#"))
{
if (color.Length == 9)
return new SolidColorBrush(
Color.FromArgb(
Convert.ToByte(color.Substring(1, 2), 16),
Convert.ToByte(color.Substring(3, 2), 16),
Convert.ToByte(color.Substring(5, 2), 16),
Convert.ToByte(color.Substring(7, 2), 16)
)
);
else
if (color.Length == 7)
return new SolidColorBrush(
Color.FromArgb(
0xff,
Convert.ToByte(color.Substring(1, 2), 16),
Convert.ToByte(color.Substring(3, 2), 16),
Convert.ToByte(color.Substring(5, 2), 16)
)
);
}
else
{
Type colorType = (typeof(System.Windows.Media.Colors));
if (colorType.GetProperty(color) != null)
{
object o = colorType.InvokeMember(color,
System.Reflection.BindingFlags.GetProperty, null, null, null); if (o != null)
{
return new SolidColorBrush((Color)o);
}
}
}
return new SolidColorBrush(Colors.Transparent);
}
SolidColorBrush c1 = GetColorFromString("Red");
SolidColorBrush c2 = GetColorFromString("#ffff0000");
SolidColorBrush c3 = GetColorFromString("#ff0000");
To naprawdę nie jest z ciągiem jak „Czerwony ", prawda? – crdx
Naprawiono ten kod. Teraz ten przekonwertowany ciąg jak "Czerwony" –