Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using style defined in merged dictionary from another merged dictionary

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.

like image 733
Kamil N. Avatar asked Feb 08 '14 13:02

Kamil N.


1 Answers

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...

like image 185
Kamil N. Avatar answered Nov 20 '22 20:11

Kamil N.