Translating C# Brushes to Silverlight Brushes

If you’ve read my blog post Translating C# Color to Silverlight Color, you’re aware that there are fewer color constants in Silverlight than there are in C#. The post above demonstrates how to work around that – but the same problem exists for Brushes.

There are a great number of brushes that we can use in C# (static properties in the Brushes class), but in Silverlight there are like… none. I can’t even find the Brushes class in Silverlight. But never worry, I’ll use Reflection to create a BrushConstants class that we can use in both C# and Silverlight (if you use my ColorConstants class from my previous blogpost).

I used this code to generate the class;

        static void Main(string[] args)
        {
            List<PropertyInfo> propertyInfos =
                typeof(Color)
                    .GetProperties(BindingFlags.Static | BindingFlags.Public)
                    .Where(prop => prop.PropertyType == typeof(Color))
                    .ToList();

            List<string> properties =
                propertyInfos
                    .Select(
                    prop => string.Format(
                                "public static Brush {0} {{get{{return new SolidColorBrush(ColorConstants.{0});}}}}",
                                prop.Name))
                    .ToList();

            File.WriteAllLines(@"c:\slask\properties.txt", properties.ToArray());
        }

The finished class is presented below. Note that in this case, you can use the same class in both Silverlight and C#, the reason is that the ColorConstants class is available in both – even though they differ they can be used the same;

using System.Windows.Media;

public static class BrushConstants
{
    public static Brush Transparent { get { return new SolidColorBrush(ColorConstants.Transparent); } }
    public static Brush AliceBlue { get { return new SolidColorBrush(ColorConstants.AliceBlue); } }
    public static Brush AntiqueWhite { get { return new SolidColorBrush(ColorConstants.AntiqueWhite); } }
    public static Brush Aqua { get { return new SolidColorBrush(ColorConstants.Aqua); } }
    public static Brush Aquamarine { get { return new SolidColorBrush(ColorConstants.Aquamarine); } }
    public static Brush Azure { get { return new SolidColorBrush(ColorConstants.Azure); } }
    public static Brush Beige { get { return new SolidColorBrush(ColorConstants.Beige); } }
    public static Brush Bisque { get { return new SolidColorBrush(ColorConstants.Bisque); } }
    public static Brush Black { get { return new SolidColorBrush(ColorConstants.Black); } }
    public static Brush BlanchedAlmond { get { return new SolidColorBrush(ColorConstants.BlanchedAlmond); } }
    public static Brush Blue { get { return new SolidColorBrush(ColorConstants.Blue); } }
    public static Brush BlueViolet { get { return new SolidColorBrush(ColorConstants.BlueViolet); } }
    public static Brush Brown { get { return new SolidColorBrush(ColorConstants.Brown); } }
    public static Brush BurlyWood { get { return new SolidColorBrush(ColorConstants.BurlyWood); } }
    public static Brush CadetBlue { get { return new SolidColorBrush(ColorConstants.CadetBlue); } }
    public static Brush Chartreuse { get { return new SolidColorBrush(ColorConstants.Chartreuse); } }
    public static Brush Chocolate { get { return new SolidColorBrush(ColorConstants.Chocolate); } }
    public static Brush Coral { get { return new SolidColorBrush(ColorConstants.Coral); } }
    public static Brush CornflowerBlue { get { return new SolidColorBrush(ColorConstants.CornflowerBlue); } }
    public static Brush Cornsilk { get { return new SolidColorBrush(ColorConstants.Cornsilk); } }
    public static Brush Crimson { get { return new SolidColorBrush(ColorConstants.Crimson); } }
    public static Brush Cyan { get { return new SolidColorBrush(ColorConstants.Cyan); } }
    public static Brush DarkBlue { get { return new SolidColorBrush(ColorConstants.DarkBlue); } }
    public static Brush DarkCyan { get { return new SolidColorBrush(ColorConstants.DarkCyan); } }
    public static Brush DarkGoldenrod { get { return new SolidColorBrush(ColorConstants.DarkGoldenrod); } }
    public static Brush DarkGray { get { return new SolidColorBrush(ColorConstants.DarkGray); } }
    public static Brush DarkGreen { get { return new SolidColorBrush(ColorConstants.DarkGreen); } }
    public static Brush DarkKhaki { get { return new SolidColorBrush(ColorConstants.DarkKhaki); } }
    public static Brush DarkMagenta { get { return new SolidColorBrush(ColorConstants.DarkMagenta); } }
    public static Brush DarkOliveGreen { get { return new SolidColorBrush(ColorConstants.DarkOliveGreen); } }
    public static Brush DarkOrange { get { return new SolidColorBrush(ColorConstants.DarkOrange); } }
    public static Brush DarkOrchid { get { return new SolidColorBrush(ColorConstants.DarkOrchid); } }
    public static Brush DarkRed { get { return new SolidColorBrush(ColorConstants.DarkRed); } }
    public static Brush DarkSalmon { get { return new SolidColorBrush(ColorConstants.DarkSalmon); } }
    public static Brush DarkSeaGreen { get { return new SolidColorBrush(ColorConstants.DarkSeaGreen); } }
    public static Brush DarkSlateBlue { get { return new SolidColorBrush(ColorConstants.DarkSlateBlue); } }
    public static Brush DarkSlateGray { get { return new SolidColorBrush(ColorConstants.DarkSlateGray); } }
    public static Brush DarkTurquoise { get { return new SolidColorBrush(ColorConstants.DarkTurquoise); } }
    public static Brush DarkViolet { get { return new SolidColorBrush(ColorConstants.DarkViolet); } }
    public static Brush DeepPink { get { return new SolidColorBrush(ColorConstants.DeepPink); } }
    public static Brush DeepSkyBlue { get { return new SolidColorBrush(ColorConstants.DeepSkyBlue); } }
    public static Brush DimGray { get { return new SolidColorBrush(ColorConstants.DimGray); } }
    public static Brush DodgerBlue { get { return new SolidColorBrush(ColorConstants.DodgerBlue); } }
    public static Brush Firebrick { get { return new SolidColorBrush(ColorConstants.Firebrick); } }
    public static Brush FloralWhite { get { return new SolidColorBrush(ColorConstants.FloralWhite); } }
    public static Brush ForestGreen { get { return new SolidColorBrush(ColorConstants.ForestGreen); } }
    public static Brush Fuchsia { get { return new SolidColorBrush(ColorConstants.Fuchsia); } }
    public static Brush Gainsboro { get { return new SolidColorBrush(ColorConstants.Gainsboro); } }
    public static Brush GhostWhite { get { return new SolidColorBrush(ColorConstants.GhostWhite); } }
    public static Brush Gold { get { return new SolidColorBrush(ColorConstants.Gold); } }
    public static Brush Goldenrod { get { return new SolidColorBrush(ColorConstants.Goldenrod); } }
    public static Brush Gray { get { return new SolidColorBrush(ColorConstants.Gray); } }
    public static Brush Green { get { return new SolidColorBrush(ColorConstants.Green); } }
    public static Brush GreenYellow { get { return new SolidColorBrush(ColorConstants.GreenYellow); } }
    public static Brush Honeydew { get { return new SolidColorBrush(ColorConstants.Honeydew); } }
    public static Brush HotPink { get { return new SolidColorBrush(ColorConstants.HotPink); } }
    public static Brush IndianRed { get { return new SolidColorBrush(ColorConstants.IndianRed); } }
    public static Brush Indigo { get { return new SolidColorBrush(ColorConstants.Indigo); } }
    public static Brush Ivory { get { return new SolidColorBrush(ColorConstants.Ivory); } }
    public static Brush Khaki { get { return new SolidColorBrush(ColorConstants.Khaki); } }
    public static Brush Lavender { get { return new SolidColorBrush(ColorConstants.Lavender); } }
    public static Brush LavenderBlush { get { return new SolidColorBrush(ColorConstants.LavenderBlush); } }
    public static Brush LawnGreen { get { return new SolidColorBrush(ColorConstants.LawnGreen); } }
    public static Brush LemonChiffon { get { return new SolidColorBrush(ColorConstants.LemonChiffon); } }
    public static Brush LightBlue { get { return new SolidColorBrush(ColorConstants.LightBlue); } }
    public static Brush LightCoral { get { return new SolidColorBrush(ColorConstants.LightCoral); } }
    public static Brush LightCyan { get { return new SolidColorBrush(ColorConstants.LightCyan); } }
    public static Brush LightGoldenrodYellow { get { return new SolidColorBrush(ColorConstants.LightGoldenrodYellow); } }
    public static Brush LightGreen { get { return new SolidColorBrush(ColorConstants.LightGreen); } }
    public static Brush LightGray { get { return new SolidColorBrush(ColorConstants.LightGray); } }
    public static Brush LightPink { get { return new SolidColorBrush(ColorConstants.LightPink); } }
    public static Brush LightSalmon { get { return new SolidColorBrush(ColorConstants.LightSalmon); } }
    public static Brush LightSeaGreen { get { return new SolidColorBrush(ColorConstants.LightSeaGreen); } }
    public static Brush LightSkyBlue { get { return new SolidColorBrush(ColorConstants.LightSkyBlue); } }
    public static Brush LightSlateGray { get { return new SolidColorBrush(ColorConstants.LightSlateGray); } }
    public static Brush LightSteelBlue { get { return new SolidColorBrush(ColorConstants.LightSteelBlue); } }
    public static Brush LightYellow { get { return new SolidColorBrush(ColorConstants.LightYellow); } }
    public static Brush Lime { get { return new SolidColorBrush(ColorConstants.Lime); } }
    public static Brush LimeGreen { get { return new SolidColorBrush(ColorConstants.LimeGreen); } }
    public static Brush Linen { get { return new SolidColorBrush(ColorConstants.Linen); } }
    public static Brush Magenta { get { return new SolidColorBrush(ColorConstants.Magenta); } }
    public static Brush Maroon { get { return new SolidColorBrush(ColorConstants.Maroon); } }
    public static Brush MediumAquamarine { get { return new SolidColorBrush(ColorConstants.MediumAquamarine); } }
    public static Brush MediumBlue { get { return new SolidColorBrush(ColorConstants.MediumBlue); } }
    public static Brush MediumOrchid { get { return new SolidColorBrush(ColorConstants.MediumOrchid); } }
    public static Brush MediumPurple { get { return new SolidColorBrush(ColorConstants.MediumPurple); } }
    public static Brush MediumSeaGreen { get { return new SolidColorBrush(ColorConstants.MediumSeaGreen); } }
    public static Brush MediumSlateBlue { get { return new SolidColorBrush(ColorConstants.MediumSlateBlue); } }
    public static Brush MediumSpringGreen { get { return new SolidColorBrush(ColorConstants.MediumSpringGreen); } }
    public static Brush MediumTurquoise { get { return new SolidColorBrush(ColorConstants.MediumTurquoise); } }
    public static Brush MediumVioletRed { get { return new SolidColorBrush(ColorConstants.MediumVioletRed); } }
    public static Brush MidnightBlue { get { return new SolidColorBrush(ColorConstants.MidnightBlue); } }
    public static Brush MintCream { get { return new SolidColorBrush(ColorConstants.MintCream); } }
    public static Brush MistyRose { get { return new SolidColorBrush(ColorConstants.MistyRose); } }
    public static Brush Moccasin { get { return new SolidColorBrush(ColorConstants.Moccasin); } }
    public static Brush NavajoWhite { get { return new SolidColorBrush(ColorConstants.NavajoWhite); } }
    public static Brush Navy { get { return new SolidColorBrush(ColorConstants.Navy); } }
    public static Brush OldLace { get { return new SolidColorBrush(ColorConstants.OldLace); } }
    public static Brush Olive { get { return new SolidColorBrush(ColorConstants.Olive); } }
    public static Brush OliveDrab { get { return new SolidColorBrush(ColorConstants.OliveDrab); } }
    public static Brush Orange { get { return new SolidColorBrush(ColorConstants.Orange); } }
    public static Brush OrangeRed { get { return new SolidColorBrush(ColorConstants.OrangeRed); } }
    public static Brush Orchid { get { return new SolidColorBrush(ColorConstants.Orchid); } }
    public static Brush PaleGoldenrod { get { return new SolidColorBrush(ColorConstants.PaleGoldenrod); } }
    public static Brush PaleGreen { get { return new SolidColorBrush(ColorConstants.PaleGreen); } }
    public static Brush PaleTurquoise { get { return new SolidColorBrush(ColorConstants.PaleTurquoise); } }
    public static Brush PaleVioletRed { get { return new SolidColorBrush(ColorConstants.PaleVioletRed); } }
    public static Brush PapayaWhip { get { return new SolidColorBrush(ColorConstants.PapayaWhip); } }
    public static Brush PeachPuff { get { return new SolidColorBrush(ColorConstants.PeachPuff); } }
    public static Brush Peru { get { return new SolidColorBrush(ColorConstants.Peru); } }
    public static Brush Pink { get { return new SolidColorBrush(ColorConstants.Pink); } }
    public static Brush Plum { get { return new SolidColorBrush(ColorConstants.Plum); } }
    public static Brush PowderBlue { get { return new SolidColorBrush(ColorConstants.PowderBlue); } }
    public static Brush Purple { get { return new SolidColorBrush(ColorConstants.Purple); } }
    public static Brush Red { get { return new SolidColorBrush(ColorConstants.Red); } }
    public static Brush RosyBrown { get { return new SolidColorBrush(ColorConstants.RosyBrown); } }
    public static Brush RoyalBlue { get { return new SolidColorBrush(ColorConstants.RoyalBlue); } }
    public static Brush SaddleBrown { get { return new SolidColorBrush(ColorConstants.SaddleBrown); } }
    public static Brush Salmon { get { return new SolidColorBrush(ColorConstants.Salmon); } }
    public static Brush SandyBrown { get { return new SolidColorBrush(ColorConstants.SandyBrown); } }
    public static Brush SeaGreen { get { return new SolidColorBrush(ColorConstants.SeaGreen); } }
    public static Brush SeaShell { get { return new SolidColorBrush(ColorConstants.SeaShell); } }
    public static Brush Sienna { get { return new SolidColorBrush(ColorConstants.Sienna); } }
    public static Brush Silver { get { return new SolidColorBrush(ColorConstants.Silver); } }
    public static Brush SkyBlue { get { return new SolidColorBrush(ColorConstants.SkyBlue); } }
    public static Brush SlateBlue { get { return new SolidColorBrush(ColorConstants.SlateBlue); } }
    public static Brush SlateGray { get { return new SolidColorBrush(ColorConstants.SlateGray); } }
    public static Brush Snow { get { return new SolidColorBrush(ColorConstants.Snow); } }
    public static Brush SpringGreen { get { return new SolidColorBrush(ColorConstants.SpringGreen); } }
    public static Brush SteelBlue { get { return new SolidColorBrush(ColorConstants.SteelBlue); } }
    public static Brush Tan { get { return new SolidColorBrush(ColorConstants.Tan); } }
    public static Brush Teal { get { return new SolidColorBrush(ColorConstants.Teal); } }
    public static Brush Thistle { get { return new SolidColorBrush(ColorConstants.Thistle); } }
    public static Brush Tomato { get { return new SolidColorBrush(ColorConstants.Tomato); } }
    public static Brush Turquoise { get { return new SolidColorBrush(ColorConstants.Turquoise); } }
    public static Brush Violet { get { return new SolidColorBrush(ColorConstants.Violet); } }
    public static Brush Wheat { get { return new SolidColorBrush(ColorConstants.Wheat); } }
    public static Brush White { get { return new SolidColorBrush(ColorConstants.White); } }
    public static Brush WhiteSmoke { get { return new SolidColorBrush(ColorConstants.WhiteSmoke); } }
    public static Brush Yellow { get { return new SolidColorBrush(ColorConstants.Yellow); } }
    public static Brush YellowGreen { get { return new SolidColorBrush(ColorConstants.YellowGreen); } }
}

About mfagerlund
Writes code in my sleep - and sometimes it even compiles!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: