Tuesday, August 11, 2009

Nesting implicit styles in WPF

All I wanted to do was define a style at Page level to control the font size of Label controls, and then define other styles for parts of the page where Label controls need to be further customised.

e.g. set font size to 8pt at the top-level, and for some parts of the page, set each label to a specific width.

I Google-d around and thought the Style BasedOn attribute should do it for me.

Alas, it appeared not to.

But then I noticed a major bug in the VS.NET 2008 XAML designer : it was not properly applying style changes!

If I changed the text in a Label control, the XAML designer would reflect that, but if I changed relevant styles, the XAML designer would often miss it.

And that's where I realised : BasedOn IS the key, it's just that the VS.NET XAML designer is so pathetic that you have to close and re-open any XAML document you are editing in order to see the effect of nested style changes.

And of course, to make matters worse, VS.NET is famous for its idiotically slow load times for XAML files in large projects that contain lots of XAML files. So, hello lots more time being wasted now that we're forced to close and re-open XAML files just to get the designer to refresh properly...

Clarity Consulting wrote a nice little piece showing how to use BasedOn with nested implicit styles. The missing piece of the puzzle is simply the need to close and re-open the XAML designer each time you make a change. Ugggh. But at least it eventually gets to the desired effect...