Countdown to Silverlight 3 #3: Merged resource dictionaries

by Andrej Tozon 28. June 2009 07:57

Coming from the WPF world, finding out that Silverlight lacked the feature of distributing application resources among separate files was one of those kick in a head moments. Well, not anymore. Silverlight 3, like its big brother WPF, now supports partitioning resources through merged dictionaries.

Merged resource dictionaries can be included as external files (build action = Content), as included resources (build action = Resource), or as referenced assembly resources (e.g. theming).

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/Theme1.xaml" />
<ResourceDictionary Source="Themes/Theme2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

Merged resource dictionaries come in useful when:

  • providing different themes for the application (like in the sample below)
  • providing a new style/template for existing control
  • providing a style/template for a custom control

Run the sample online

Source code below:

Tags:

Silverlight | Development

Comments

7/4/2009 6:39:15 AM #

maxima

Could you please elaborate on : "Merged resource dictionaries come in useful when... providing a new style/template for existing control"?

Thank you

maxima United Kingdom | Reply

7/4/2009 8:58:13 AM #

Andrej

maxima,
similarly to my last point, I like to have a separate resource file for each control I restyle (or just retemplate) to keep them separate for faster access.
When changing the look of a let's say, ListBox, I usually start out from it's default style/template, make the changes I need, then put the new style in a separate resource file and include it where needed (usually in app.xaml).
Hope that answers your question.

Andrej Slovenia | Reply

7/19/2009 4:18:50 AM #

slyi

You could also use singleton that implements inotifypropertychanged and use converter to change the styles rather than walking the visual tree.
eg: cid-289eaf995528b9fd.skydrive.live.com/.../MDic.zip

slyi Ireland | Reply

7/19/2009 7:50:09 AM #

maxima

Yes that answered the question.

Slyi - thanks for the code. this idea was demonstraded on the MS show recently.

I currently use custom upgraded Toolkit Theming to achieve the same...

maxima United Kingdom | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading





Andrej Tozon

MVP - Client Application Developer

Microsoft Certified Solution Developer

MSN Alerts

View Andrej Tozon's profile on LinkedIn

Subscribe to me on FriendFeed

Andrej Tozon's Facebook profile

Get help from Andrej Tozon!

RecentComments

Comment RSS