Helper method to get a color based on it's string value
using System.Windows.Media;
public static class Utilities
{
/// <summary>
/// Helper method to get a color based on it's string value
/// </summary>
/// <param name = "colorString">The name of the string, for example red</param>
/// <returns>A brush with the color, bla</returns>
public static Brush StringToBrush(string colorString)
{
Brush returnValue = Brushes.Black;
if (!string.IsNullOrEmpty(colorString))
{
string tempString = colorString.ToLower().Trim();
if (tempString.EndsWith("_anime"))
tempString = tempString.Replace("_anime", "");
switch (tempString)
{
case "grey":
returnValue = Brushes.Gray;
break;
case "red":
returnValue = Brushes.Red;
break;
case "blue":
returnValue = Brushes.Green;
break;
}
}
return returnValue;
}
}
Related examples in the same category