MikaelKP
MikaelKP

Reputation: 133

Each dictionary entry must have an associated key error in WPF project

I get the following error in my App.xaml file: "Each dictionary entry must have a key"

My App.xaml looks like this:

<Application.Resources>
    <DataTemplate DataType="{x:Type Model:ClassData}">
        <Canvas>
            <View:ClassDataUserControl/>
        </Canvas>
    </DataTemplate>

    <ResourceDictionary> <----- ERROR HERE
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/UMLDiagram.Windows.Theme;component/Theme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

    <DataTemplate DataType="{x:Type Model:Connector}">
        <Canvas>
            <View:ConnectorUserControl/>
        </Canvas>
    </DataTemplate>
</Application.Resources>

My Resource Dictionary (ButtonStyle.xaml) is placed in another project called UMLDiagram.Windows.Theme and the code looks like this:

<Style TargetType="Button">
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                <GradientStop Color="#FFF3F3F3" Offset="0"/>
                <GradientStop Color="#FFEBEBEB" Offset="0.5"/>
                <GradientStop Color="#FFDDDDDD" Offset="0.5"/>
                <GradientStop Color="#FF456DB4" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
 </Style> 

To make the software scaleable the ButtonStyle.xaml is called via Theme.xaml (also placed in UMLDiagram.Windows.Theme project)

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="ButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>

I have tried everything and I cannot see why I see the error!

Can anybody please help me with this problem?

Best regards, Mikael

Upvotes: 3

Views: 7121

Answers (1)

Michael G
Michael G

Reputation: 6745

You need to put your DataTemplate inside of the ResourceDictionary.

When you create a new resource dictionary reference as a resource, all resources must be contained in that resource dictionary.

You can read more about using the merged resource dictionary here.

<Application.Resources>

  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="/UMLDiagram.Windows.Theme;component/Theme.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <DataTemplate DataType="{x:Type Model:ClassData}">
      <Canvas>
        <View:ClassDataUserControl/>
      </Canvas>
    </DataTemplate>

    <DataTemplate DataType="{x:Type Model:Connector}">
      <Canvas>
        <View:ConnectorUserControl/>
      </Canvas>
    </DataTemplate>
  </ResourceDictionary>

</Application.Resources>

Upvotes: 10

Related Questions