Loading XAML from C# – HACK!!!!

Ever wondered how to convert XAML to C# or to load a huge XAML content from C#, this post will possibly give you the best workaround to load it :).

If you have large Brush values or large XAML content that you would want to instantiate and apply it dynamically thru C#, it is either you convert to string and use the XAML reader or convert everything to some equivalent C# code. One other way is you could put up the XAML content inside a resource dictionary and hook it into the generic.xaml file. For more info on generic.xaml you can check out creating control templates for WPF, which allows overriding templates through resource files.

Once you have the resource dictionary setup with the XAML content, Say as below,

<?xml version="1.0" encoding="utf-8"?>
<ResourceDictionary xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DrawingBrush x:Key="Asterisk">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <DrawingGroup.Children>
                    <GeometryDrawing Brush="#FF000000" Geometry="F1 M 416.193,288.721L 406.118,
300.568L 406.212,300.848L 421.043,297.863L 421.043,307.284L 406.118,304.673L 406.025,304.952L 
416.193,316.426L 407.891,321.277L 402.574,306.818L 402.387,306.911L 396.977,321.277L 388.954,316.426L 
398.936,304.766L 398.936,304.579L 384.29,307.284L 384.29,297.863L 398.749,300.755L 398.842,
300.568L 388.954,288.814L 397.257,284.057L 402.48,298.329L 402.667,298.329L 407.984,284.057L 416.193,
288.721 Z "/>
                </DrawingGroup.Children>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>
</ResourceDictionary>

The above is a Asterisk symbol drawn with brush values. You could then create a WPF control instance and use the brush value as a background. Lets do that for a Border control,

private static ResourceDictionary GetResourceDictionary()
{
    ResourceDictionary rd = new ResourceDictionary();
    rd.Source = GetXamlConvertedValue<Uri>("Window1;component/Themes/Generic.xaml");
    return rd;
}

The above code simply creates a new Resource dictionary with the specified URI path. The GetXamlConvertedValue is simply a utility function that converts the string into the appropriate instance using TypeConverter,

public static T GetXamlConvertedValue<T>(string value)
{
    var converter = TypeDescriptor.GetConverter(typeof(T));
    if (converter != null)
    {
        return (T)converter.ConvertFromInvariantString(value);
    }
    return default(T);
}

 

Now we get the ResourceDictionary and get the DrawingBrush using the key “Asterisk”,

public static Brush BackgroundBrush
{
    get
    {
        var rd = GetCellRendererDictionary();
        var brush = rd["Asterisk"] as Brush;
        return brush;
    }
}

 

That’s it, We can simply get the value and set it as a background to the Border control. Like so any other content can be placed inside the ResourceDictionary and access it thru the Key/Value pair.

 

-Fahad

Advertisements

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: