How do I convert a string value to a SolidColorBrush in Silverlight and C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LoanB
    New Member
    • Nov 2007
    • 62

    How do I convert a string value to a SolidColorBrush in Silverlight and C#

    Hi team,

    I have a silverlight application in which I am drawing a rectangle. The background of this rectangle must be set according to a color stored in a SDE database. The field in the database is a string and stored e.g. "Green".

    I retrieve the string from the SDE database:

    var col = "StoredcolorVal ue." + RoadBridgeData. Attributes["COLOR"].ToString();

    But I dont know how the set the code below so that the part in brackets (Colors.Storedc olorValue) will use my color from the database.

    The normal way to di it is useing one of the systems colors like below:

    Rectangle.Fill = new SolidColorBrush (Colors.Green);

    Any help will be appreciated.
  • LoanB
    New Member
    • Nov 2007
    • 62

    #2
    I have found a solution to this:

    using System.Reflecti on;

    // Returns a System.Windows. Media.Colors value from a input string of name of Color.

    public Color GetThisColor(st ring colorString)
    {
    Type colorType = (typeof(System. Windows.Media.C olors));
    if (colorType.GetP roperty(colorSt ring) != null)
    {
    object o = colorType.Invok eMember(colorSt ring,
    BindingFlags.Ge tProperty, null, null, null); if (o != null)
    {
    return (Color)o;
    }
    }
    return Colors.Transpar ent;
    }

    Comment

    Working...