WPF in Story of Resources.

As I started to play with WPF and XAML one of first things was to get some free vector images. Those images came already formated as XAML resources:

<ResourceDictionary ...>
<DrawingImage x:Key="Horizon_Image_Up1">
...
</DrawingImage>
</ResourceDictionary>

All one needed to do was to include them as resource. I personally like to do that at application level:

<Application ...>
<Application.Resources>
<ResourceDictionary Source="Resources/Left1.xaml" />
<ResourceDictionary Source="Resources/Down1.xaml" />
<ResourceDictionary Source="Resources/Up1.xaml" />
</Application.Resources>
</Application>

Once we do this there will be error message greeting: "Property 'Resources' accepts only one object."

There are two solutions to this problem. First one is obvious - put all resources in same file. I personally tend to avoid it since I like to reuse resources in multiple projects and I hate carrying bunch of unused images.

Second solution is to merge all those dictionaries into one. Fortunately, there is simple way to do it:

<Application ...>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<Application.Resources>
<ResourceDictionary Source="Resources/Left1.xaml" />
<ResourceDictionary Source="Resources/Down1.xaml" />
<ResourceDictionary Source="Resources/Up1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

With that you have single dictionary consisting of all individual images. Do notice that, if there is same key defined in different files, only image that was defined last will be used.

One thought to “WPF in Story of Resources.”

  1. Very nice it helped me allot with a hick up i had. Only i was trying to merge 2 ResourceDictionary but instead in the App.xaml i tried in the Window1.xaml as i only took out the Window1 from WPF project and inserted in WinForms application (therefore no need for App.xaml). The one thing i got stuck was there where other things in the Window.Resources and kept getting the “Property ‘Resources’ accepts only one object.” error. Solved it like this:
    BEFORE:

Leave a Reply

Your email address will not be published. Required fields are marked *