I’m developing a map application in Silverlight. I have a type Polygon, containing the coordinates of a polygon together with a string containing the name of Brush resource loaded into the application. I’m binding this object directly to a template for drawing polygons. This template has a Fill-propery of type System.Windows.Media.Brush.
My goal is to let the xaml-parser do the lookup of the resource, considering I only have the name of the Brush and not the resource itself.
In Silverlight I want to achieve something similar to what Pedro Sampaio is showing in WPF in this blog post: http://www.e-pedro.com/2009/06/using-data-binding-with-static-resources-in-wpf/. He has created a BindableStaticResource inheriting the StaticResourceExtension-type. Then he can do the binding like this:
<Polygon Fill="{BindableStaticResource {Binding NameOfFillResource}}" />
Very elegant! But it appears that this is not an option in Silverlight since there are no MarkupExtension-type to subtype. I’m fairly new to Silverlight, and can’t really think of a good, reusable way of implementing this.
Thanks!
HaraldV
Your are correct there is no way create your own markup extensions for silverlight.
I solve this sort of problem with a value converter. I’ve answered this sort of thing so often I ended up blogging it here.
With the source for
StringToObjectConverterin you project your can place an instance of the converter in your resources somewhere:-then your binding will look like:-