15

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?

Mike S.
  • 402
  • 1
  • 3
  • 13

3 Answers3

14

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>
Alexander
  • 2,320
  • 2
  • 25
  • 33
Luke Forder
  • 1,179
  • 14
  • 20
  • 1
    This does work and I understand why it resolves now but why does it not resolve when it is in the higher level dictionary? I am marking your answer as correct, but I would love an explanation if you know why this is. – Mike S. Feb 29 '12 at 20:20
  • Found this in another thread. Leaving here for posterity. https://social.msdn.microsoft.com/forums/windowsapps/en-US/2be9a5f6-5313-448d-a9d9-296bac42215e/using-style-defined-in-merged-dictionary-from-another-merged-dictionary?forum=wpdevelop Thanks and credit to Kamil Nieweglowski for this. – Snouto May 15 '15 at 10:51
9

You can include one dictionary in another (like 'using' in C#) like so:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
    xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:Controls="clr-namespace:APC.IKM.UI.SL.Controls"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Brushes.xaml"/>
        <ResourceDictionary Source="Fonts.xaml"/>
        <ResourceDictionary Source="CoreStyles.xaml"/>
    </ResourceDictionary.MergedDictionaries>

Is this what you are looking for? The Cosmopolitan / Metro project template has a good example of this...

Adriaan Davel
  • 710
  • 1
  • 11
  • 25
0

the trully answer is Eric's answer in this site: https://social.msdn.microsoft.com/forums/windowsapps/en-US/2be9a5f6-5313-448d-a9d9-296bac42215e/using-style-defined-in-merged-dictionary-from-another-merged-dictionary?forum=wpdevelop.
the Brushes.xaml and the NavigationPanel.xaml is parsed independently and then added to the merged dictionary of Application resources so they don't know anything about each other.

GIANGPZO
  • 380
  • 2
  • 3
  • 21