views:

71

answers:

2

In my MainWindow.xaml, I have the following reference to a ResourceDictionary:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MainSkin.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

In MainSkin.xaml, I define a datatemplate:

<DataTemplate x:Key="TagTemplate">
   ...
</DataTemplate>

Deeper within my application, I attempt to use this data template:

<ContentControl DataContext="{Binding Tag}" ContentTemplate="{StaticResource TagTemplate}"/>

The code compiles successfully, but when I attempt to load a Page or UserControl that contains this StaticResource, I get an exception saying that the TagTemplate can't be found.

What am I doing wrong?

A: 

Are you using that StaticResource in the same Window where it is declared? Otherwise I think that you cannot have access to that.

Maurizio Reginelli
Yes. I'm using the ContentControl in Page displayed within a Frame on MainWindow.
dthrasher
+1  A: 

In order to access the contents of a resource defined in a XAML file, you need to "include" that XAML file in each page and control that uses it. So every XAML files will need to have the MergedDictionaries entry that you have in MainWindow.xaml.

Alternatively you can add those merge dictionaries to App.xaml and those resources are included implicitly:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MainSkin.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
Igor Zevaka
So UserControls or DataTemplates used inside my MainWindow don't have access to items defined in a merged dictionary referenced in MainWindow's Window.Resources section? I have to go up one level to the Application Resources instead?
dthrasher
Yes, the resources that defined in a resource dictionary in a control are only accessible to that control/page.
Igor Zevaka