Using style defined in merged dictionary from another merged dictionary
c#, windows-phone-7, windows-phone-8, wpf, xaml
Solution
This is a response to my question from Erick Fleck at msdn forums:
In your first example each file is parsed independently and then added to the merged dictionary so they don't know anything about each other...similarly, the XAML in a merged dictionary cannot reference names in the 'parent' ResourceDictionary. In other words, you should think of a MergedDictionaries as a one-way reference.
It's the way It works I guess...
Problem
Below you can see how I am trying to segregate styles by merging dictionaries (I'm skipping namespaces for the sake of cleanliness) App.xaml: ``` <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Style/Colors.xaml" /> <ResourceDictionary Source="Style/HeaderStyle.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> ``` Colors.xaml: ``` <SolidColorBrush x:Key="DarkTextForeground" Color="#7471b9"/> ``` HeaderStyle.xaml: ``` <Style x:Key="HeaderTextBlockStyle" TargetType="TextBlock"> <Setter Property="Foreground" Value="{StaticResource DarkTextForeground}"/> <Setter Property="FontWeight" Value="Black"/> </Style> ``` During compilation I get a following error: Cannot find a Resource with the Name/Key DarkTextForeground To make It work we have to merge Colors.xaml inside HeaderStyle.xaml like this: ``` <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Colors.xaml"/> </ResourceDictionary.MergedDictionaries> <Style x:Key="HeaderTextBlockStyle" TargetType="TextBlock"> <Setter Property="Foreground" Value="{StaticResource DarkTextForeground}"/> <Setter Property="FontWeight" Value="Black"/> </Style> ``` Can anyone explain to me, why I have to reference Colors.xaml in HeaderStyle.xaml? Can't I just reference styles defined in different merged dictionary? I assume that Colors.xaml is loaded before HeaderStyle.xaml so it should be visible for dictionaries defined later.