Thursday, March 15, 2012

OK - I know I've not posted in nearly a year... but this time I'm covering one of those "why the frack does it work like that" issues with Silverlight 4.

I hit this one today - I wanted to unify the radii of corners within a Silverlight application to replace the random set of radii used (1, 2, 4, 5, 8, 10, 12, 15 and 20 pixels no less) with a simpler set - small, medium, large and massive.

The problem is that (for whatever reason), the developers of Silverlight decided that we didn't need to be able to create CornerRadius objects in our ResourceDictionaries. Or rather that we could TRY, but that the XAML Loader was never told how to deal with them. Even though the design surfaces in Visual Studio and Expression Blend can quite happily deal with this!

So if you *DO* try then you either get an error saying that you can't set the readonly TopLeft property of a CornerRadius, or if, like me, you do this in your theme.xaml you just get a catastrophic error - somewhere in your project.

So what was the solution?

Well, there was a very good answer to exactly this on Stack Overflow - using a Value Converter to read named resource values from the object for which we want to set the CornerRadius... But I suddenly realised that there was no need to have a "magic string" as keys for the TopLeft etc values - we can just use properties on the value converter ITSELF!

The result is a pretty elegant way to get around a limitation of Silverlight, thus: