Convert String to Color in Silverlight

Posted on 2012-09-01
Last Modified: 2014-11-12
I am building a windows phone app that the user is able to put in a hex value like "FFFFFF" and the value that they put in gets recalculated to a couple related values, each of which is displayed as text.

I am trying to make rectangles next to them that change to the color they state.however, i am having issues setting the colors to the string values. How do i go about this? I have googled around, and Haven't gotten very far :/

There are some great ways introduced in .Net 3.0, but they are not in silverlight.

Thanks in advance
Question by:dashifire
    LVL 55

    Accepted Solution


    Author Closing Comment

    Yeah, looks like there isn't an inherent way to to do it. I ended up having to do custom coding similar to your third link. I found it online, but am struggling t find the page i got it from...

    Author Comment

    Here is the code
     public static SolidColorBrush GetBrushFromHexString(string aarrggbb)
                String xamlString = "<Canvas xmlns=\"\" Background=\"" + aarrggbb + "\"/>";
                Canvas c = (Canvas)System.Windows.Markup.XamlReader.Load(xamlString);
                return (SolidColorBrush)c.Background;
            private Color GetColorFromHexa(string hexaColor)
                return 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));

    Open in new window

    I use this to invoke it
    Brush colorname = GetBrushFromHexString("#FF" + textBlock3.Text);

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now