Why can't I find the DataTemplates in the combined resource dictionaries?

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

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

      

More deeply in my application, I am trying to use this data template:

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

      

The code compiles successfully, but when I try to load a page or UserControl containing this StaticResource, I get an exception saying the TagTemplate could not be found.

What am I doing wrong?

+2


a source to share


2 answers


To access the content of a resource that is defined in a XAML file, you need to "include" that XAML file on every page and control that uses it. Therefore, for every XAML file, you need to have an entry MergedDictionaries

that you have in MainWindow.xaml.

Alternatively, you can add these merge dictionaries to App.xaml

, and these resources are implicitly included:



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

      

+3


a source


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



0


a source







All Articles