Referencing a resource in a ResourceDictionary from a different ResourceDictionary in Silverlight

Mike S. picture Mike S. · Feb 29, 2012 · Viewed 16.7k times · Source

I have the following set of code in my App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml"/>
            <ResourceDictionary Source="/Client.Common;component/Theme/Fonts.xaml"/>
            <ResourceDictionary Source="/Client.Common;component/Theme/CoreStyles.xaml"/>
            <ResourceDictionary Source="/Client.Common;component/Theme/SdkStyles.xaml"/>
            <ResourceDictionary Source="/Client.Common;component/Theme/MyAppName.xaml"/>

            <ResourceDictionary Source="/Client.Common;component/Controls/NavigationPanel.xaml"/>
         </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

The NavigationPanel.xaml contains a style that looks like this:

<Style x:Key="NavigationPanelListBox" TargetType="ListBox">
    <Setter Property="Background" Value="{StaticResource DarkBackground}" />
    <Lots of XAML>
</Style>

The {StaticResource DarkBackground} is defined in the Brushes.xaml file (i.e. the first resource dictionary). It is defined as

<SolidColorBrush x:Key="DarkBackground" Color="#FF707176" />

in the resource dictionary.

At runtime, I get the following error:

Cannot find a Resource with the Name/Key DarkBackground [Line: 16 Position: 44]

The line numbers and position references the NavigationPanel.xaml resource dictionary in the app.xaml.

I can reference the brush from other controls, just not the included resource dictionary.

Why can I not reference or why does it not resolve the reference to a resource that is higher in the heirarchy of the merged resource dictionary?? What am I missing here?

Answer

Luke Forder picture Luke Forder · Feb 29, 2012

Are you referencing the DarkBackground brush in any of the resources in the NavigationPanel dictionary?

If you are you might need to merge the Brushes resource dictionary into the NavigationPanel dictionary.

So in the NavigationPanel dictionary.

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml" />
</ResourceDictionary.MergedDictionaries>