9

I've created control styles I want to use among multiple xaml pages in my WPF app. To do this I created a Resources.xaml and added the styles there.

Then in my pages I add this code

<Grid.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/SampleEventTask;component/Resources.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Grid.Resources>

On two pages this works fine, but on the 3rd page I get a compile error that says:

All objects added to an IDictionary must have a Key attribute or some other type of key associated with them.

If I add a key to this, as such ResourceDictionary x:Key="x", then the compile error goes but on running the app it errors finding the style.

I can make the compile error go away and have the app run by just moving original (no key specified) "ResourceDictionary" xaml from the top level Grid into a contained Grid on that page.

But I don't understand what is going on here. Any suggestions as to what the problem is, I'm just missing something or doing something incorrectly. Is there a better way to share styles?

thanks

Gern Blanston
  • 42,482
  • 19
  • 50
  • 64
  • possible duplicate of [Can you define a ResourceDictionary.MergedDictionary together with other (local) resources in ?](http://stackoverflow.com/questions/2695849/can-you-define-a-resourcedictionary-mergeddictionary-together-with-other-local) – Dzyann Jun 03 '15 at 13:59

2 Answers2

8

Are there any other resources defined other than the merged ResourceDictionary in that Page?

For example, here's a snippet from a Window I created.

<Window x:Class="SelectionPagePrototype.SelectionPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SelectionPagePrototype"
    Title="SelectionPage" MinHeight="600" MinWidth="800" Loaded="OnLoaded">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ImageResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <local:QuickPickCheckedConverter x:Key="quickPickConverter" />
            <local:BoolToCaptionConverter x:Key="captionConverter" />
            <local:ProductAndImageTypeConverter x:Key="imageConverter" />
        </ResourceDictionary>
    </Window.Resources>
    <Grid> ...

The merged dictionary did not require a key, but the other resources for that Window do.

Eric Olsson
  • 4,805
  • 32
  • 35
0

In my case, the issue was that the was not the first item in the <Grid.Resources>, so even if all of the other items had a Key defined, the error was popping up at compile time. By moving the Resource Dictionary as the first item in the resources, the project compiles successfully.

Bruno
  • 1
  • 3